-
June 23rd, 2010, 12:50 PM
#1
StreamWriter?
Hello.
I was wondering if someone could tell me why my script isn't working?
I expect it to write "Hello word" to log.txt on my desktop?
Code:
public struct _cnf
{
public string logS;
}
static void form1(_cnf cnf)
{
cnf.logS = "C:\\Users\\Tom\\Desktop\\log.txt";
StreamWriter logF = new StreamWriter(cnf.logS);
logF.WriteLine("Hello World");
}
There are no compiling errors, it just doesn't write anything in log.txt
any ideas?
thanks.
-
June 23rd, 2010, 12:56 PM
#2
Re: StreamWriter?
Close the streamwriter or call Flush on it. Your application is exiting before the internal buffer in StreamWriter has been flushed to disk.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
June 23rd, 2010, 12:56 PM
#3
Re: StreamWriter?
You are never flushing you stream. You write to the stream, which is in memory, but you never flush it to disk and you also do not dispose of it. Use this:
Code:
using ( StreamWriter writer = new StreamWriter( @"C:\foo.txt" ) )
{
writer.Write( "Hello" );
writer.Flush( );
}
EDIT: Ninja'd
-
June 23rd, 2010, 01:16 PM
#4
Re: StreamWriter?
Hello
Thank you for your help! works now
It prints my message to the file, but it puts the cursor to the very beggining, so If I print more, it tends to overwrite it I think? How do I make it go at the bottom so it prints the next lot below it?
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|