//日志
string strCAD = "";
strCAD += args+ ";\r\n";
string _fullPath = string.Format(@"{0}", "D://文件名//");
if (!Directory.Exists(_fullPath))
{
Directory.CreateDirectory(_fullPath);
}
string _extensionName = ".txt";
string fullFilename = _fullPath +args.name+ _extensionName;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
sw.Write(strCAD, Encoding.UTF8);
sw.Dispose();
}
注意,args是接收的参数,里面包含多个参数, strCAD += args+ ";\r\n";表示向这个txt文件中写入的值,路径在D盘,
(2)下面是可以按照每一天分成不同的文件夹,在里面创建不同时间的txt文件。注意listPo是list泛型
string JsonData = JsonConvert.SerializeObject(listPo);
string strCAD = JsonData + ";\r\n";
// strCAD += JsonData + ";\r\n";
string _fullPath = string.Format(@"{0}", "D://文件名//");
string _extensionName = ".txt";
string d = DateTime.Now.ToString("yyyy-MM-dd hh时mm分ss秒");
string yd = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" + "//";
string path = _fullPath + yd;//当前路径
//查看是否有当前的路径:目的是为了分别给每一天建一个文件夹。
if (Directory.Exists(path))
{
string fullFilename = path + d + _extensionName;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}
else
{//
string NewPath = _fullPath + yd;
Directory.CreateDirectory(NewPath);
string fullFilename = NewPath + d + _extensionName;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}
下面的路径是没有写死的
string _fullPath = System.Web.HttpContext.Current.Server.MapPath("~/Log/BDCtoTDC");//p3的路径D:\DIC\MES
// string p2 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;//D:\\项目\Yunnsoo\\DIC_Yunnsoo\\DL201704\\
string JsonData = JsonConvert.SerializeObject(args);
string strCAD = JsonData + ";\r\n";
string _extensionName = ".txt";
string txt = DateTime.Now.ToString("yyyy-MM-dd hh时mm分ss秒")+_extensionName;
string yd = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" + "\\";
//string path = _fullPath + yd;//当前路径
string path = Path.Combine(_fullPath, yd);
//查看是否有当前的路径:目的是为了分别给每一天建一个文件夹。
if (Directory.Exists(path))
{
string fullFilename = path + txt;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}
else
{//
string NewPath = _fullPath+"\\" + yd;
Directory.CreateDirectory(NewPath);
string fullFilename = NewPath + txt;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}
下面是在服务中获取地址的几种写法
string exeFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string txtFile = Path.Combine(exeFolder, "test.txt");
string ss = AppDomain.CurrentDomain.BaseDirectory;
string key = @"SYSTEM\CurrentControlSet\Services\MyService1";
string path1 = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();
下面是根据天数分隔的:
string _fullPath = "";
_fullPath = System.Web.HttpContext.Current.Server.MapPath("~/Log/In_Out_CountLog");
// string p2 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
string strCAD = "内容"+";\r\n";
string _extensionName = ".txt";
string txt = " "_extensionName;
string yd = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "\\";
//string path = _fullPath + yd;//当前路径
string path = Path.Combine(_fullPath, yd);
//查看是否有当前的路径:目的是为了分别给每一天建一个文件夹。
if (Directory.Exists(path))
{
string fullFilename = path + txt;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}
else
{//
string NewPath = _fullPath + "\\" + yd;
Directory.CreateDirectory(NewPath);
string fullFilename = NewPath + txt;
using (StreamWriter sw = new StreamWriter(fullFilename, true))
{
try
{
sw.Write(strCAD);
sw.Dispose();
}
catch (FormatException e)
{
throw e;
}
}
}