C# 如果不存在,则创建目录
我想为不同的操作制作日志。我每天创建一个以日期为文件名的新文件。现在,如果目录不存在,我希望系统为我创建目录。我搜索了这个主题,所有的答案都是一样的:使用C# 如果不存在,则创建目录,c#,logging,asp.net-mvc-5,C#,Logging,Asp.net Mvc 5,我想为不同的操作制作日志。我每天创建一个以日期为文件名的新文件。现在,如果目录不存在,我希望系统为我创建目录。我搜索了这个主题,所有的答案都是一样的:使用Directory.CreateDirectory(FilePath)。然而,这似乎不起作用。可能遗漏了一些明显的东西 代码如下: public class ElderlyHomeLog : ILog { private const string FilePath = "/Logs/WZCLogs/";
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();
}
}
}
错误消息:
发生“System.IO.DirectoryNotFoundException”类型的异常
在mscorlib.dll中,但未在用户代码中处理
其他信息:找不到路径的一部分
“C:\Users\***\Source\Repos\Project\ProjectName\Logs\WZCLogs\31032016.txt”
文件夹可以在C:\
(安装操作系统的默认驱动器)中创建。这就是文件夹的位置是C:\Logs\WZCLogs\
。您可以通过再次执行代码来确认是否在驱动器中的某个位置创建了文件夹,这一次如果(!Directory.Exists(FilePath))
返回true
。由于您尚未指定编译器假定的任何位置。检查是否已创建
您可以像这样扩展try:
try
{
Directory.CreateDirectory(FilePath);
}
catch (Exception ex)
{
// handle them here
}
如果路径错误,肯定会抛出异常;我尝试过使用“X:\sample”,这给了我一个例外:
找不到路径“X:\sample”的一部分
而如果我尝试使用Logs\WZCLogs
,第一次不会出现任何异常,第二次也会跳过if;因此,我发现文件夹是在其他地方创建的
您可以进行以下更改以使其正常工作:
string FilePath=Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"Logs\WZCLogs");
创建目录时需要使用绝对路径。请尝试以下操作:
private const string FilePath = "Logs/WZCLogs/";
public void MakeLog(string text)
{
string directory = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, FilePath);
Directory.CreateDirectory(directory); // no need to check if it exists
string logFile = Path.Combine(directory, DateTime.Now.ToString("ddMMyyyy") + ".txt");
if (!File.Exists(logFile))
{
FileStream f = File.Create(logFile);
f.Close();
}
using (StreamWriter sw = new StreamWriter(logFile, true))
{
sw.WriteLine(text);
sw.Close();
}
}
您不需要首先检查目录是否存在,因为如果目录已经存在,CreateDirectory
方法没有副作用。使用路径也是一种很好的做法。组合而不是直接连接字符串,但确保第二个参数不以斜杠开头
您还可以使用File.AppendAllText
方法简化代码,而不是创建FileStream
private const string FilePath = "Logs/WZCLogs/";
public void MakeLog(string text)
{
string directory = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, FilePath);
Directory.CreateDirectory(directory);
string logFile = Path.Combine(directory, DateTime.Now.ToString("ddMMyyyy") + ".txt");
File.AppendAllText(logFile, text + Environment.NewLine);
}
错误消息或说明?您是否尝试为其提供要创建的目录的绝对路径?另外,如果使用Path.Combine
组合路径,则不必担心目录分隔符。FilePath变量可能不完整?是否确实需要重新发明方轮?那么呢?另外,您是否考虑过将日志文件命名为“yyyyMMdd”,以便按名称排序也按日期排序?这解决了我的问题,谢谢。更改了Directory.CreateDirectory()代码>到目录.CreateDirectory(HostingEnvironment.ApplicationPhysicalPath+FilePath)代码>//在指定路径中创建所有目录和子目录,除非它们已经存在。公共静态目录信息创建目录(字符串路径)mkdir
不是C#函数,在这里没有帮助
char str[] = "folder_name";
//if directory doesn't exist then create it
mkdir(str);