| View previous topic :: View next topic |
| Author |
Message |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Mon Feb 23, 2009 5:20 am Post subject: [C#]TextWriter.Write fails |
|
|
Ok, I'm having a file that I want to write some information to, but whenever I try to write to it with a string containing a filepath, the operation fails. Why is this? Any help is greatly appreciated.
Edit: Typo in title.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Spawnfestis GO Moderator
Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Mon Feb 23, 2009 7:44 am Post subject: |
|
|
Details? Code? Errors?
Roll fucking eyes.
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Mon Feb 23, 2009 7:50 am Post subject: |
|
|
There is no error, the text is simply not written to the file. I didn't think the code would be needed considering the only interesting part is the call itself:
| Code: | StreamWriter someStream = new TextWriter(filename);
someStream.Write(theStringToWrite); |
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
iNoobHacker Advanced Cheater
Reputation: 0
Joined: 05 Nov 2006 Posts: 99
|
Posted: Mon Feb 23, 2009 7:58 am Post subject: |
|
|
| Close the file...
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Mon Feb 23, 2009 8:02 am Post subject: |
|
|
I'm sorry if I mislead you, the code is actually like this : | Code: | using(StreamWriter someStream = new TextWriter(filename))
{
someStream.Write(theStringToWrite);
} |
Now correct me if I'm wrong, but doesn't this automatically close the stream after the closing bracket?
Edit: Typo.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Last edited by Odecey on Mon Feb 23, 2009 8:14 am; edited 1 time in total |
|
| Back to top |
|
 |
iNoobHacker Advanced Cheater
Reputation: 0
Joined: 05 Nov 2006 Posts: 99
|
Posted: Mon Feb 23, 2009 8:09 am Post subject: |
|
|
| The object will be disposed but the Close method will not be called automatically, I belive.
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Mon Feb 23, 2009 8:12 am Post subject: |
|
|
Adding someStream.Close(); did not have any effect on the programs behaviour that I could see. I suspected this, considering it works fine when saving strings that does not contain filepaths.
Edit: Made a workaround for it. A part of the problem seems to have been that the default directory changes when using FileDialogs.
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Tue Feb 24, 2009 5:36 am Post subject: |
|
|
| Odecey wrote: | There is no error, the text is simply not written to the file. I didn't think the code would be needed considering the only interesting part is the call itself:
| Code: | StreamWriter someStream = new TextWriter(filename);
someStream.Write(theStringToWrite); |
|
TextWriter is abstract you should get an error with that code.
| iNoobHacker wrote: | | The object will be disposed but the Close method will not be called automatically, I belive. |
When you use 'using' it will be closed automatically.
You can create a new instance of a streamwriter and write to the file.
| Code: | using (StreamWriter someStream = new StreamWriter(@"C:\test\lol.txt"))
{
someStream.Write("hijax");
} |
That will erase the file whenever it attempts to write to the file tho, if you want to append just send a bool like this :
| Code: | using (StreamWriter someStream = new StreamWriter(@"C:\test\lol.txt", true))
{
someStream.Write("jihad");
} |
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
|