How to create a file or folder C#
public static void DataLog(string message) {
string path = @"C:\DataLog\\data\\";
if (Directory.Exists(path)) {
StreamWriter sw = null;
try {
string logFile = DateTime.Now.ToString("ddMMyyyy") + "data.txt";
sw = new StreamWriter(@"C:\DataLog\\data\\" + logFile, true);
sw.WriteLine(DateTime.Now.ToString() + " : " + message);
sw.Flush();
sw.Close();
}
catch (Exception ex) {
Console.WriteLine("Failed! : Cannot write log . . .");
}
}
else {
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully");
}
}
Rujukan
Directory.CreateDirectory Method - https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory?view=netcore-3.1
Ulasan