5

Is there any way to save a file with a variable name?

For example, I have this code but I got error in the StreamWriter:

string hour = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Now.ToShortDateString();
string filename = "Numbers " + date + " " + hours+ ".txt";
string path = @"C:\ShowMySms\" + filename;

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true)) {
    file.WriteLine("test");
}

I also tried use

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ShowMySms\" + filename, true))

but both ways give me an error.

Is there any way for me to export files with a different using StreamWriter?

Thank you in advance!

Edit: the error is

The given path's format is not supported.

I tried do

Path.Combine(path, filename);

and put the path in the StreamWriter but still getting the same error...

João Silva
  • 531
  • 4
  • 21
  • 40
  • 3
    If you get an error - **please POST** the full and exact error details here! We can't see your screen - nor can we read your mind - you'll need to **provide** those details here, by posting them! – marc_s Nov 18 '13 at 14:48
  • 7
    You can't use colons (`:`) in filenames, which the exception probably already tells you. Replace them with dots, for example. – CodeCaster Nov 18 '13 at 14:52
  • I posted the error... you mean the **:** in **"C:\"**? – João Silva Nov 18 '13 at 14:53
  • Put a breakpoint before where the error occurs and inspect the `path` variable. – CodeCaster Nov 18 '13 at 14:54
  • It's true, you cannot use : O \ in filename. Try to convert the time and the date using the separator - . – Stefano Bafaro Nov 18 '13 at 14:54
  • 1
    @JoãoSilva After a drive letter is one of the few places where `:` are allowed -- `C:\...`. But, they can't be used later in the path for a directory or file's own name -- `.ToString("HH:mm:ss")`. – Jonathan Lonowski Nov 18 '13 at 14:55
  • the name of path is **"C:\\ShowMySms\\Numbers 18-11-2013 14:52:15.txt"** after the **Path.combine** – João Silva Nov 18 '13 at 14:55
  • possible duplicate of [how can i format the file name like it includes date and time and month and year](http://stackoverflow.com/questions/7742953/how-can-i-format-the-file-name-like-it-includes-date-and-time-and-month-and-year) – CodeCaster Nov 18 '13 at 14:57
  • 2
    Given my earlier comment _"you can't use colons (`:`) in filenames"_ and your response _"the name of path is [..] 14 **:** 52 **:** 15.txt"_, do you now see the problem? – CodeCaster Nov 18 '13 at 15:00
  • I notice now that you are talking about the hours and not in the **"C:\"** Thank you for the help! – João Silva Nov 18 '13 at 15:05

2 Answers2

10

Try using this formatting instead:

string filename = string.Format("Numbers_yyyyMMdd_HHmmss.txt", date);

This will give you file names like

Numbers_20131118_155410.txt

which contain no "dangerous" and illegal characters (like :) in your file name ...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Just notice now that the **:** that peopla are talking about now is about the time and not in the **"C:\"**... Thank you! – João Silva Nov 18 '13 at 15:04
0

you can also try below code snippet

string filename = "Numbers" + DateTime.Now.ToString("dd-MM-yyyy_HHmmss")+ ".txt";

Output:

Numbers06-02-2018_122446.txt
Neltech101
  • 29
  • 4