3

I am trying to write all my data to a text file and it is working unless I put DateTime in the file name.
The current code looks like this:

string time = DateTime.Now.ToString("d");
string name = "MyName";
File.WriteAllText(time+name+"test.txt","HelloWorld");

I am getting this exception:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

But as far as I know, the File.WriteAllText() method should create a new file or overwrite already existed file.

Any suggestions?

M463
  • 2,003
  • 3
  • 23
  • 39
threesixnine
  • 1,733
  • 7
  • 30
  • 56

4 Answers4

8

You might want to make sure that the path is valid and the datetime string does not contain invalid characters:

string time = DateTime.Now.ToString("yyyy-MM-dd"); 

  // specify your path here or leave this blank if you just use 'bin' folder
string path = String.Format(@"C:\{0}\YourFolderName\", time);

string filename = "test.txt"; 

// This checks that path is valid, directory exists and if not - creates one:
if(!string.IsNullOrWhiteSpace(path) && !Directory.Exist(path)) 
{
   Directory.Create(path);
}

And finally write you data to a file:

File.WriteAllText(path + filename,"HelloWorld");
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • 1
    please see my or Maynak's answer. You should not use the culture depending 'shortcuts' when you want to create a filename. – Steffen Winkler Jul 29 '15 at 08:10
  • Still not there. No need to do a Directory check. If the filename is valid, the file would be written to bin directory in debug mode or the program's folder in Release mode. We just need to make sure filename is valid. – Mayank Jul 29 '15 at 08:23
  • @Mayank Why are you so sure that author just wants to use the 'bin' directory ? If he specifies the full path with directory that does not exist (as he probably would) he'll got an exception. – Fabjan Jul 29 '15 at 08:28
  • 1
    Because that does not answer OP's question. OP is asking why he is getting an exception when specifying a correct (according to him) file name. He has not mentioned any directory in his question. So we can safely assume that he knows where to find the file in case no directory is specified. Why confuse him with Directory checks? – Mayank Jul 29 '15 at 08:33
5

According to the MSDN a DateTime.Now.ToString("d") looks like this: 6/15/2008(edit: depending on your local culture it could result in a valid filename)

Slashes are not valid in a filename.

Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
  • 1
    while that is correct (in de-DE culture it should result in a valid format -> 15.06.2008) I'd guess and say that Mystia is using a culture that uses a delimiter that is not allowed in a filename. – Steffen Winkler Jul 29 '15 at 08:04
3

replace

string time = DateTime.Now.ToString("d");
File.WriteAllText(time+name+"test.txt","HelloWorld");

with

string time = DateTime.Now.ToString("yyyyMMdd_HHmmss"); // clean, contains time and sortable
File.WriteAllText(@"C:\yourpath\" + time+name + "test.txt","HelloWorld");

you have to specify the whole path - not just the filename

fubo
  • 44,811
  • 17
  • 103
  • 137
  • 1
    while it isn't exactly 'nice' to not use a fullpath, using just the filename should still create that file 'somewhere'. (Usually in the directory where the application is located) – Steffen Winkler Jul 29 '15 at 08:01
  • @Mystia fubo recently edited his answer and added a '@' in front of the filepath. Either add it to your code or add a second backslash since you've to escape a backslash in astring. – Steffen Winkler Jul 29 '15 at 08:07
2

This is because your name would be resolving to "7/29/2015MyNametest.txt" or something else which contains an invalid character depending on the culture of your machine. The example that I gave is obviously not a valid file path. We have to remove the slashes (/). They are not allowed in file name on Windows.

See this question as a comprehensive file naming guideline on Windows and Linux.

Community
  • 1
  • 1
Mayank
  • 1,621
  • 2
  • 14
  • 30