Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to export a file.

Below is the code for the path.

C#
 string date = Convert.ToString(DateTime.Now);

string namefile = "Stock_Flow_Report_2015" + date;


When i remove date, it is working fine. But when I put the date it shows the exception that The given format is not supported.

Please help regarding this.
Posted

Quite a lot of characters are illegal in for names: ? * \ and/ fit example.

And the default date time format on your system is likely to be dd/MM/yyyy or MM/dd/yyyy

So try this:
C#
string namefile = "Stock_Flow_Report_2015" + DateTime.Now.ToString("dd-MM-yyyy");
Or better:
C#
string namefile = DateTime.Now.ToString("yyyy-MM-dd") +  "Stock_Flow_Report_2015;
 
Share this answer
 
Comments
Member 9017207 30-Oct-15 3:47am    
Thanks man. Much appreciated. Solved my problem.
OriginalGriff 30-Oct-15 4:01am    
You're welcome!
What you want could be achieved in one single line, without having to create a secondary string variable:
C#
// C# 5-
string namefile = string.Format("Stock_Flow_Report_2015{0}", DateTime.Now);

// OR
// C# 6
string namefile = string.Format($"Stock_Flow_Report_2015{DateTime.Now}");


string.format() will take care of converting the DateTime value to string.

Hope this helps.
 
Share this answer
 
Comments
Member 9017207 30-Oct-15 3:13am    
Still didnot solve the issue. Works fine on my local machine. But not after I publish the application in server
phil.o 30-Oct-15 3:16am    
Then the problem is not where you think it is. Above code would work anywhere, provided the .NET Framework is installed.
And 'does not work fine' is not a valid issue description.
Member 9017207 30-Oct-15 3:19am    
I get the exception. The given path's format is not supported.

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NotSupportedException: The given path's format is not supported.
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at _80IB_GUI.Form1.ExportTheFileAfterProcess(String sqlquery, String namefile)
at _80IB_GUI.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
phil.o 30-Oct-15 3:27am    
If you are trying to put in a file name a datetime value with '/' character, sure it will fail. You did not talk about any file related operation in the first place. Please see solution 2.
[no name] 30-Oct-15 4:15am    
Please test solution 4 Now .
As per my understanding in date format "/" character is coming that is not allowed in file name. So you have to use
C#
string date = DateTime.Now.ToString("dd_MM_yyyy");
string namefile = "Stock_Flow_Report_2015" + date;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900