DateTime Exception

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I am trying to add DataTime.Now.ToString() to a file name but I get an
exception while doing it that such format is not supported in path. How
can I solve this and cast this issue?

Thank you!
 
csharpula csharp pisze:
Hello,
I am trying to add DataTime.Now.ToString() to a file name but I get an
exception while doing it that such format is not supported in path. How
can I solve this and cast this issue?

Thank you!

Standard DateTime Format contains '/' signs, which are not allowed in
path, so you have to use ToString with format parameter (you can see in
MSDN help how to specify wanted format).
 
Check your filename against System.IO.Path.GetInvalidPathChars() and
System.IO.Path.GetInvalidFileNameChars(). More than likely you have at
least a couple of violations. A fix I have used is to use
DateTime.Now.Ticks.
 
Hello,
I am trying to add DataTime.Now.ToString() to a file name but I get an
exception while doing it that such format is not supported in path. How
can I solve this and cast this issue?

Thank you!

*** Sent via Developersdexhttp://www.developersdex.com***

DateTime dt = System.DateTime.Now;
string s1 = dt.ToShortDateString().ToString
(System.Globalization.CultureInfo.CurrentCulture);
string s2 = dt.ToString
(System.Globalization.CultureInfo.CurrentCulture);

s1 = s1.Replace("/", null);
s1 = s1.Replace(":", null);
s2 = s2.Replace("/", null);
s2 = s2.Replace(":", null);

Console.WriteLine(s1);
Console.WriteLine(s2);

The above code snippet may help

-Cnu
 
Duggi said:
DateTime dt = System.DateTime.Now;
string s1 = dt.ToShortDateString().ToString
(System.Globalization.CultureInfo.CurrentCulture);
string s2 = dt.ToString
(System.Globalization.CultureInfo.CurrentCulture);

s1 = s1.Replace("/", null);
s1 = s1.Replace(":", null);
s2 = s2.Replace("/", null);
s2 = s2.Replace(":", null);

Console.WriteLine(s1);
Console.WriteLine(s2);

The above code snippet may help

-Cnu

There are multiple ways to do this, obviously. One of my favorite ways,
which also enables sorting to continue through the date (in chronological
order):

DateTime.Now.ToString("yyyyMMdd_hhmmss"); // includes time
DateTime.Now.ToString("yyyyMMdd"); // does not include time

Then append one of the above methods to the file name (before adding the
extension), then add the extension and voila! Filename + valid date :)

HTH,
Mythran
 
Hello All,

Below link discuss the issue and resolution
for the error.

<a href="http://aspdotnethacker.blogspot.com/2010/05/error-paths-format-is-not-supported.html">http://aspdotnethacker.blogspot.com/2010/05/error-paths-format-is-not-supported.html</a>





Duggi wrote:

Re: DateTime Exception
16-Nov-08


DateTime dt =3D System.DateTime.Now
string s1 =3D dt.ToShortDateString().ToStrin
(System.Globalization.CultureInfo.CurrentCulture)
string s2 =3D dt.ToStrin
(System.Globalization.CultureInfo.CurrentCulture)

s1 =3D s1.Replace("/", null)
s1 =3D s1.Replace(":", null)
s2 =3D s2.Replace("/", null)
s2 =3D s2.Replace(":", null)

Console.WriteLine(s1)
Console.WriteLine(s2)

The above code snippet may hel

-Cnu

Previous Posts In This Thread:

DateTime Exception
Hello
I am trying to add DataTime.Now.ToString() to a file name but I get a
exception while doing it that such format is not supported in path. Ho
can I solve this and cast this issue

Thank you



Re: DateTime Exception
csharpula csharp pisze

Standard DateTime Format contains '/' signs, which are not allowed in
path, so you have to use ToString with format parameter (you can see in
MSDN help how to specify wanted format).

Check your filename against System.IO.Path.GetInvalidPathChars() and System.IO.
Check your filename against System.IO.Path.GetInvalidPathChars() and
System.IO.Path.GetInvalidFileNameChars(). More than likely you have at
least a couple of violations. A fix I have used is to use
DateTime.Now.Ticks


Re: DateTime Exception

DateTime dt =3D System.DateTime.Now
string s1 =3D dt.ToShortDateString().ToStrin
(System.Globalization.CultureInfo.CurrentCulture)
string s2 =3D dt.ToStrin
(System.Globalization.CultureInfo.CurrentCulture)

s1 =3D s1.Replace("/", null)
s1 =3D s1.Replace(":", null)
s2 =3D s2.Replace("/", null)
s2 =3D s2.Replace(":", null)

Console.WriteLine(s1)
Console.WriteLine(s2)

The above code snippet may hel

-Cnu

Re: DateTime Exception

There are multiple ways to do this, obviously. One of my favorite ways,
which also enables sorting to continue through the date (in chronological
order)

DateTime.Now.ToString("yyyyMMdd_hhmmss"); // includes tim
DateTime.Now.ToString("yyyyMMdd"); // does not include tim

Then append one of the above methods to the file name (before adding the
extension), then add the extension and voila! Filename + valid date :

HTH
Mythran


Submitted via EggHeadCafe - Software Developer Portal of Choice
Server Side Processing in ADO.NET/WCF Data Services
http://www.eggheadcafe.com/tutorial...f-4f6f92a76585/server-side-processing-in.aspx
 
Back
Top