Create a directory if it does not exist

I want to make magazines for different activities. I create a new file every day with the file name date. Now, if the directory does not exist, I want the system to create a directory for me. I searched this topic and all the answers come to the same thing: use Directory.CreateDirectory(FilePath); . However, this does not seem to work. Perhaps something is unclear.

Here is the code:

 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(); } } } 

Error message:

An exception of type "System.IO.DirectoryNotFoundException" occurred in mscorlib.dll, but was not processed in the user code

Additional information: Could not find part of the path 'C: \ Users \ *** \ Source \ Repos \ Project \ ProjectName \ Logs \ WZCLogs \ 31032016.txt'.

+10
source share
2 answers

A folder can be created on your C:\ (the default drive on which the OS is installed). folder location C:\Logs\WZCLogs\ . You can confirm that the folder was created somewhere on the disk by executing the code again, this time if (!Directory.Exists(FilePath)) will return true . Since you did not specify any locations, the compiler accepts So. Check if it is created or not;

You can extend the attempt like this:

 try { Directory.CreateDirectory(FilePath); } catch (Exception ex) { // handle them here } 

If the path is wrong, an exception will definitely be thrown; I tried with "X: \ sample", which gives me an exception:

Could not find part of path 'X: \ sample

Whereas if I tried with Logs\WZCLogs which would not throw any exceptions the first time, and also skip if for the second time; From here I discovered that the folder was created somewhere else;

You can make these changes to make them work:

  string FilePath=Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"Logs\WZCLogs"); 
+13
source

You need to use the absolute path when creating the directory. Try the following:

 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(); } } 

You do not need to check if the directory exists, since the CreateDirectory method has no side effects if the directory is already present. It is also recommended that you use Path.Combine rather than concatenating strings directly, but make sure the second parameter does not start with a slash.

You can also simplify your code by using the File.AppendAllText method instead of creating a 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); } 
+3
source

All Articles