Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DateTime current_time = DateTime.Now;
string date = current_time.ToString("yyyyMMddHHmm");
string filename = string.Format("{0}", "RR" + date + ".rmt");
string path = Path.Combine("C:\\", filename);
StreamWriter str = new StreamWriter(path);
while (u<modemcount)>
{ 
  if (ReadModemData[u].Datas != null)
  {
    arr[0] = digit(ReadModemData[u].Datas[0]);  //0.0.2 
    arr[1] = current_time.ToString(",yyMMdd,");
    arr[2] = current_time.ToString("HHmm,");
    arr[3] = digit2(ReadModemData[u].Datas[1]) + ",";   //5.0.     
    arr[4] = digit2(ReadModemData[u].Datas[2]) + ",";    //5.0.1
    arr[5] = digit2(ReadModemData[u].Datas[3]) + ",";      //5.0.2
    arr[6] = "-1,";
    arr[7] = digit2(ReadModemData[u].Datas[4]) + ","; //6.0.0
    arr[8] = digit(ReadModemData[u].Datas[5]);                          //8.0.1
    array = (arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5] + arr[6] + arr[7] + arr[8]);
    MessageBox.Show(array);
    str.WriteLine(array + "\r");
  } 
  else
  { str.Write(""); }
  u++; 

messagebox shows my information but text file doesn't show any thing.
Posted
Updated 3-Nov-11 22:38pm
v6
Comments
OriginalGriff 4-Nov-11 4:20am    
How have you checked your file content?
When did you check it?

First, you should wrap the StreamWriter in a using as below and you might try adding a Flush statement (though not strictly required).

using (StreamWriter str = new StreamWriter(path))
{
   // Code...

   str.Flush();
}


Does your process create an empty file or no file at all?
Also, don't see where array is declared: perhaps it should be as:
str.WriteLine(array.ToString());


In any case, consider using a StringBuilder to gather the data together: easier to format and can be used directly.

Try reading through this: StreamWriter Class[^]
 
Share this answer
 
Comments
maria anders 4-Nov-11 6:16am    
the text file creates but nothing is in.=
R. Giskard Reventlov 4-Nov-11 6:27am    
Have you tried my suggestions (and those below by LanFanNinja)
maria anders 4-Nov-11 6:43am    
your solution even doesnt build any file
maria anders 4-Nov-11 6:48am    
thanks dear mark
Ok first off as the author of Solution 1 said you need to either wrap the StreamWriter in a using statement or after the while loop call str.Close(); and str.Dispose(); to remove the StreamWriter object from memory. Second if using Windows vista or Windows 7 make sure you do not have a permissions problem creating the document at C:\filename. Third note the str.Flush(); in the code below:

C#
while (u<modemcount)>
{ 
    if (ReadModemData[u].Datas != null)
    {
        arr[0] = digit(ReadModemData[u].Datas[0]);  //0.0.2 
        arr[1] = current_time.ToString(",yyMMdd,");
        arr[2] = current_time.ToString("HHmm,");
        arr[3] = digit2(ReadModemData[u].Datas[1]) + ",";   //5.0.     
        arr[4] = digit2(ReadModemData[u].Datas[2]) + ",";    //5.0.1
        arr[5] = digit2(ReadModemData[u].Datas[3]) + ",";      //5.0.2
        arr[6] = "-1,";
        arr[7] = digit2(ReadModemData[u].Datas[4]) + ","; //6.0.0
        arr[8] = digit(ReadModemData[u].Datas[5]); //8.0.1
        array = (arr[0] + arr[1] + arr[2] + arr[3] + arr[4] + arr[5] + arr[6] + 
            arr[7] + arr[8]);
        MessageBox.Show(array);
        str.WriteLine(array + "\r");
      } 
      else
      { 
          str.Write(""); 
      }
      str.Flush();
      u++;
}
str.Close();
str.Dispose();


I am also not sure you need the + "\r" in
C#
str.WriteLine(array + "\r");
or the
C#
else
{
    str.Write("");
}
 
Share this answer
 
v2
Comments
maria anders 4-Nov-11 6:47am    
thanks dear
You need to close the StreamWriter. File output is buffered and does not get written until the writer is closed, in general. (You could flush it but that makes little sense here.) Wrapping it in a using clause as Mark suggests will cause it to get closed at the end of the using scope and should also fix your problem.

You should always put file code inside a using
using(StreamWriter sw = new StreamWriter(...)){
 // file code here
}

... or a try/finally:
StreamWriter sw = new StreamWriter(...);
try{
 // file code here
} finally {
 sw.Close();
}

... otherwise an exception in your file I/O code will result in the file handle not being closed and the file being inaccessible (including to your application) until the process terminates.

I also agree with Mark that if you are building up a string for text output like this you should use a StringBuilder. Using an array as you have makes sense if you need to use the parts separately for some reason, but you are just using it to construct the line of text so use a text-based class for the job.
 
Share this answer
 
Try not to put your file on the root of your hard drive, as UAC can prevent programs from doing this.
A good pratice is to use your user's documents folder, where you can read/write anything you want to.
 
Share this answer
 

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