Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using StreamWriter to save a file

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601
I get this error.
Error is:

Could not find a part of the path "c:\windows\system32\inetsrv\d\webs\tempFiles\10019_11262005_70420_AM.tmp".

The statement I used for stream writer is:

The statement I used for stream writer

StreamWriter sw = new StreamWriter(fileName,false);

fileName has value of d\webs\tempFiles\10019_11262005_70420_AM.tmp".

with preceding @ which I understand is like a global escape character?

What is up with this and how do I save a file where I want it to go??
 
Make sure the file exists before you write to it.

Code:
lock(this)
{
     if( !File.Exists(fileName) )
         File.Create(fileName);
}

//now write to the file
 
Thanks for your response but I don't see how this can help me.
#1
I know the file does not exist. This is a temporary file that I will write to - pass the name to another page via querystring - open up and read contents - and then delete.
#2. let me amend my first post. filename = "d:\webs ...
not d\webs...
#3. What the problem is that I am specifying I want to go to the D drive with this file and it is appending my name unto the c:\windows\system32\inetsvr folder. Why is it doing that? It does the same thing with your File.Create statement. I am about ready to switch to using the FileSystemObject (which all things I have read caution against in Dot Net).

I actually got the program to work by not specifying the D drive and letting it go to the C drive. I specify the folder and file name only and it automatically puts it on the c drive in the correct folder. I don't like that arrangement but hey, it works so far in two different versions of the web site. The last hurdle is production. If I can get this working in production, I'll buy time so I can make it work better at a later date. I still have today to come up with a fix so if you can tell me why I can't specify a path without it being changed - I'd be appreciative.
 
Oh, I gotcha now.

The problem is that the StreamWriter is interpreting your file path as relative when it is actually absolute. Can I see where you're assigning the value to fileName? That may be where the error is.

This code works fine on my box:

Code:
string fileName = @"d:\Temp\Blah.txt";
StreamWriter sw = new StreamWriter( fileName, false );

sw.Write( "blah blah blah" );
sw.Close();

Oh, and FYI, @ indicates that the program will use the string following verbatim (including line breaks, and interpreting a backslash as a backslash instead of the lead-up to an escape character).

For instance:

Code:
@"d:\Temp\Blah.txt";
...
@"text with 
line break";
[\code]

is roughly equivalent to:

[code]
"d:\\Temp\\Blah.txt";
...
"text with \nline break";
[\code]
 
By George, you've hit upon it. I was concatenating a couple of values together with a hard coded path to come up with a base file name and then replacing some unwanted characters. I hard coded the whole thing and it worked fine. I just need to sort through the concatenation to see what is making it fail. Thanks!!!!
Now - to delete the file - do I have to use FSO?

 
I simply moved my concatenation of the @ prefaced path name so it was done after I replaced all the unwanted characters from the date/time concatenation and the problem went away. I figured out (from your File.Create sample) that I can use File.Delete to delete a file. It appears that the old FSO model is built right into the Dot Net framework. So I have learned another new trick as well.
Thanks for all your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top