Dynamic fileName

how to create a file with dynamic file name

Page 1 of 1

10 Replies - 9295 Views - Last Post: 15 January 2009 - 09:00 AM Rate Topic: -----

#1 ghinwa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-January 09

Dynamic fileName

Posted 15 January 2009 - 06:50 AM

TextWriter tw = new StreamWriter(@"C:\" + fileName +".txt");
// with fileName as String



but i keep getting this error:
The given path's format is not supported
how can i add a dynamic fileName?
Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic fileName

#2 jjsaw5   User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 3,063
  • Joined: 04-January 08

Re: Dynamic fileName

Posted 15 January 2009 - 06:52 AM

Could you please post your code so we can get an idea of what is going on?




:code:
Was This Post Helpful? 0
  • +
  • -

#3 dklingman   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Dynamic fileName

Posted 15 January 2009 - 07:40 AM

what are you passing for the filename? make sure you're not using illegal characters or that you're not passing extra backslashes.
Was This Post Helpful? 0
  • +
  • -

#4 ghinwa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-January 09

Re: Dynamic fileName

Posted 15 January 2009 - 07:46 AM

View Postdklingman, on 15 Jan, 2009 - 06:40 AM, said:

what are you passing for the filename? make sure you're not using illegal characters or that you're not passing extra backslashes.


DateTime d = DateTime.Today;
int nn = 99;
string dString = d.ToString();
string nnString = nn.ToString();

String fileName = "WG" + dString + nnString;

TextWriter tw = new StreamWriter(@"C:\" + fileName +".txt");




This is what im passing !
Was This Post Helpful? 0
  • +
  • -

#5 dklingman   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Dynamic fileName

Posted 15 January 2009 - 07:54 AM

as i said before what are you passing for you're filename. if you walk through the code you'll see that the file name that you are trying to pass is the following WG1/15/2009 12:00:00 AM99
Was This Post Helpful? 0
  • +
  • -

#6 dklingman   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Dynamic fileName

Posted 15 January 2009 - 08:05 AM

here's a little help for you to prevent something like this happening.

			if (fileName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) < 0)
			{
				// YOUR CODE HERE
			}


Was This Post Helpful? 0
  • +
  • -

#7 ghinwa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-January 09

Re: Dynamic fileName

Posted 15 January 2009 - 08:09 AM

View Postdklingman, on 15 Jan, 2009 - 06:54 AM, said:

as i said before what are you passing for you're filename. if you walk through the code you'll see that the file name that you are trying to pass is the following WG1/15/2009 12:00:00 AM99


ok thanks now i got it, i will remove the special characters from the file name

thanks again for ur help.
Was This Post Helpful? 0
  • +
  • -

#8 dklingman   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Dynamic fileName

Posted 15 January 2009 - 08:11 AM

no problem and anytime!
Was This Post Helpful? 0
  • +
  • -

#9 ghinwa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-January 09

Re: Dynamic fileName

Posted 15 January 2009 - 08:13 AM

View Postghinwa, on 15 Jan, 2009 - 07:09 AM, said:

View Postdklingman, on 15 Jan, 2009 - 06:54 AM, said:

as i said before what are you passing for you're filename. if you walk through the code you'll see that the file name that you are trying to pass is the following WG1/15/2009 12:00:00 AM99


ok thanks now i got it, i will remove the special characters from the file name

thanks again for ur help.


ok thanks for the if statement i will use it.
one more question what can i do to end the file name with .001
for example filename will be: Hello.001.txt will that be possible ?
Was This Post Helpful? 0
  • +
  • -

#10 dklingman   User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: Dynamic fileName

Posted 15 January 2009 - 08:21 AM

if you want the file name to end the .001 as the extension remove the .txt

or is that not what you're asking?
Was This Post Helpful? 0
  • +
  • -

#11 ghinwa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-January 09

Re: Dynamic fileName

Posted 15 January 2009 - 09:00 AM

View Postdklingman, on 15 Jan, 2009 - 07:21 AM, said:

if you want the file name to end the .001 as the extension remove the .txt

or is that not what you're asking?

YES THIS IS WHAT I WANTED AND IT'S WORKING FINE NOW :)

THANKS
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1