'How to resolve the exception System.IO.IOException: 'the filename, directory name, or volume label syntax is incorrect in C#?

 public ActionResult Download(DateTime? downloaddate)
    {
        {
            string path1 = AppDomain.CurrentDomain.BaseDirectory + "logs/";
            string filename = "industrialcomm-payload-" + downloaddate + ".txt";
            byte[] filebytes1 = System.IO.File.ReadAllBytes(path1 + filename);
            string contentType1 = "text/plain";

            return File(filebytes1, contentType1);
        }

I get an exception on filebytes1 and I'm wondering how to resolve the issue. I have parameter called downloaddate which is a calendar date picker in HTML. Every time I pick a date and click the button, I get an exception System.IO.IOException: 'the filename, directory name, or volume label syntax is incorrect. How can I resolve this issue?



Solution 1:[1]

I created a variable called Date and set is as a New DateTime. Then, referenced the variable Date to downloaddate parameter.

Here's the code:

DateTime Date = new DateTime();
Date = (DateTime)downloaddate;

string path1 = AppDomain.CurrentDomain.BaseDirectory + "logs/";
string filename = "industrialcomm-payload-" + Date.ToString("yyyyMMdd") + ".log";
byte[] filebytes1 = System.IO.File.ReadAllBytes(path1 + filename);
string contentType1 = "text/plain";

return File(filebytes1, contentType1, filename);

This will set the calender downloaddate to DateTime().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Csala