TXT没有显示我的信息. [英] TXT doesn't show my informtion................HELP ME

查看:79
本文介绍了TXT没有显示我的信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

首先,应将StreamWriter包装在如下的using中,您可以尝试添加Flush语句(尽管并非严格要求).

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());



无论如何,请考虑使用StringBuilder将数据收集在一起:易于格式化并且可以直接使用.

尝试通读以下内容: StreamWriter类 [



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[^]


首先,作为解决方案1的作者说,您需要将StreamWriter包装在using语句中,或者在while循环调用str.Close()之后;和str.Dispose();从内存中删除StreamWriter对象.其次,如果使用Windows Vista或Windows 7,请确保在C:\ filename创建文档时没有权限问题.第三注str.Flush();在下面的代码中:

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:

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

str.WriteLine(array + "\r");


else
{
    str.Write("");
}


您需要关闭StreamWriter.通常,文件输出被缓冲,并且直到关闭编写器后才被写入. (您可以刷新它,但在这里没有什么意义.)按照Mark的建议将其包装在using子句中,将导致它在using范围的结尾处被关闭,并且还应该解决您的问题.

您应该始终将文件代码放入using
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();
}


...否则,文件I/O代码中的异常将导致文件句柄不被关闭,并且直到进程终止,文件才不可访问(包括您的应用程序).

我也同意Mark的观点,如果您要为这样的文本输出构建字符串,则应该使用StringBuilder.如果由于某种原因需要单独使用各部分,则可以使用数组,这很有意义,但是您只是在使用它来构造文本行,因此可以使用基于文本的类来完成作业.


... 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.


这篇关于TXT没有显示我的信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆