0

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

        }
7
  • 2
    You can't use colons in the path Commented Jun 2, 2017 at 9:01
  • 2
    You only need one slash if you use @ Commented Jun 2, 2017 at 9:02
  • 2
    your path should be like: @"C:\Reminder_Logs\" Commented Jun 2, 2017 at 9:02
  • 1
    but when i remove "cTime" variable then everything ok.. Commented Jun 2, 2017 at 9:03
  • 2
    as suggested by Romano, you must not use colons try this string cTime = DateTime.Now.ToString("HH_mm").Trim(); Commented Jun 2, 2017 at 9:04

3 Answers 3

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.