我想为不同的操作制作日志.我每天创建一个新文件,日期为文件名.现在,如果目录不存在,我希望系统为我创建目录.我搜索过这个主题,所有答案都是相同的:使用Directory.CreateDirectory(FilePath);.然而,这似乎不起作用.可能会遗漏一些明显的东西.
这是代码:
public class ElderlyHomeLog : ILog
{
private const string FilePath = "/Logs/WZCLogs/";
public void MakeLog(string text)
{
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
string logFile = DateTime.Now.ToString("ddMMyyyy") + ".txt";
if (!File.Exists(HostingEnvironment.ApplicationPhysicalPath + FilePath + logFile))
{
FileStream f = File.Create(HostingEnvironment.ApplicationPhysicalPath + FilePath + logFile);
f.Close();
}
using (StreamWriter sw = new StreamWriter(HostingEnvironment.ApplicationPhysicalPath + FilePath + logFile, true))
{
sw.WriteLine(text);
sw.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
mscorlib.dll中出现"System.IO.DirectoryNotFoundException"类型的异常,但未在用户代码中处理
附加信息:找不到路径'C:\ Users\***\Source\Repos\Project\ProjectName\Logs\WZCLogs\31032016.txt'的一部分.
我需要尽快打开并读取数千个文件。
我对13 592个文件进行了一些测试,发现方法1略快于方法2。这些文件通常在800字节和4kB之间。我想知道是否可以做些什么来使此I / O绑定过程更快?
Method 1:
Run 1: 3:05 (don't know what happened here)
Run 2: 1:55
Run 3: 2:06
Run 4: 2:02
Method 2:
Run 1: 2:04
Run 2: 2:08
Run 3: 2:04
Run 4: 2:12
Run Code Online (Sandbox Code Playgroud)
这是代码:
public class FileOpenerUtil
{
/// <summary>
///
/// </summary>
/// <param name="fullFilePath"></param>
/// <returns></returns>
public static string ReadFileToString(string fullFilePath)
{
while (true)
{
try
{
//Methode 1
using (StreamReader sr = File.OpenText(fullFilePath))
{
string fullMessage = "";
string s;
while ((s …Run Code Online (Sandbox Code Playgroud)