cSharma
cSharma

Reputation: 645

Getting Error while creating file in C#

I am getting error to create a file While used string variable as current time in filename. Error as

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

Additional information: The given path's format is not supported.

I couldn't find what is wrong in the code and I tried without "path" variable it will successfully run. I didn't understand what is happening when I used "cTime" variable for create dynamic filename.

static void Main(string[] args)
        {
            string path = @"C:\\Reminder_Logs\\";
            string cTime = DateTime.Now.ToString("HH:mm").Trim();
            using (StreamWriter srRun = File.AppendText(path + "log_Reminder_" + cTime + ".txt"))
            {
                using (StreamWriter sr = File.AppendText(path + "log_Start.txt"))
                {
                    sr.WriteLine("reminder file  created!!! " + DateTime.Now.ToString("HH:mm"));
                }
            }

        }

Upvotes: 0

Views: 380

Answers (3)

Mehmet Topçu
Mehmet Topçu

Reputation: 1364

The problem in your code is that, path contains : you can try this

string cTime = DateTime.Now.ToString("HH.mm").Trim();

Upvotes: 1

Gowri Pranith Kumar
Gowri Pranith Kumar

Reputation: 1685

: char is not accepted in the file path path.getinvalidfilenamechars() gives all the invalid chars in the file path

Upvotes: 1

pitersmx
pitersmx

Reputation: 957

The : character cannot be used in file name, please use

string cTime = DateTime.Now.ToString("HHmm").Trim(); for example instead.

Upvotes: 2

Related Questions