Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code

FileStream ftoken = new FileStream("c:\\Tokens.txt",FileMode.Create);
ftoken.Close();
Regex re = new Regex(@"([\.\=\(\)\ \'])", RegexOptions.IgnorePatternWhitespace);
string classPart = " ";
ArrayList tokenArray = new ArrayList();
StreamWriter write = new StreamWriter("c:\\Tokens.txt");
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
     string[] splitString = re.Split(richTextBox1.Lines[i]);
     foreach(string s in splitString)
     {
           string token = classPart + " " + s + " " + (i + 1).ToString();
           tokenArray.Add(token);
     }
}
     foreach (object line in tokenArray)
     {
          write.WriteLine(line);
     }


i dont get any error but token.txt file has nothing :(
Posted

In addition to what Rob said: you need to use the using statement:

C#
using (StreamWriter writer = new StreamWriter("my_file_name.txt")) {
    writer.Write("blah blah blah");
    //...
} //at this point, writer.Dispose will be called even if an exception was thrown


You should use this construct for all temporary object implementing the interface System.IDisposable. It is fully equivalent to a try-finally block calling IDisposable.Dispose implemented in the object in the "finally" section of code.
See:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^].

—SA
 
Share this answer
 
Comments
Simon Bang Terkildsen 25-Aug-11 20:15pm    
ah yeah, using using is easy to forget when not in the comfortable environment of an IDE :)
+5
Sergey Alexandrovich Kryukov 25-Aug-11 21:31pm    
Why not? I have IDE with me, often use to try things before posting.
Thank you, Simon.
--SA
That's a crazy bit of code! Firstly, you don't need the FileStream at the top to create the file, the StreamWriter will do that for you. Did you put a break point on the WriteLine to check if anything is getting written?

Probably the problem is that you don't close the StreamWriter. It could well be that what you've written is sitting in a buffer and will not get put to disk until you Flush() or Close() the stream.

Also, don't use an ArrayList, that's a thing of the past. Use a List<string>, or better still just do the write rather than use a collection at all.
 
Share this answer
 
v3
Comments
Sweety Khan 25-Aug-11 12:36pm    
o yes u r right, i didnt close the streamwriter. thanx n more thanx for telling me extra things :)
Sergey Alexandrovich Kryukov 25-Aug-11 19:46pm    
Agree with you. I voted 4 -- there is more to it, an important part -- IDisposable and using "using". Anyway, I credit your answer.
Please see my solution.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900