Search code examples
c#asp.netvisual-studio-2010export-to-excel

The given path's format is not supported


I am trying to downlaod an excel sheet and I am dynamically generating its filename which has to be in the following format:

eg:

User_Wise_List_Of_Documents_2013_On_16_04_2013

For this I wrote the following code:

string currentDate = DateTime.Now.Date.ToString();

string currentYear=DateTime.Now.Year.ToString();

filename = Server.MapPath("~/User/Documents/") + "User_Wise_List_Of_Documents_" + currentYear + "on" + currentDate + ".xls";

Somehow, its giving me the following exception:

The given path's format is not supported.

Any help will be appreciated.


Solution

  • I think your filename contains invalid chars like : Because you are forming filename with string currentDate = DateTime.Now.Date.ToString().

    See the list for invalid chars

    var invalidChars = Path.GetInvalidFileNameChars();
    

    EDIT

    You can use this to replace invalid chars

    string newdatestr = String.Join("",currentDate.Select(c => invalidChars.Contains(c) ? '_' : c));