1 string txt = @"C:\DetectFolder\IPV4地址.txt";
2 string path = "";
3
4 if (File.Exists(txt)) { }//检测是否存在text文件,如果没有,则自动创造一个
5 else
6 {
7 FileStream fs = new FileStream(txt, FileMode.Create, FileAccess.Write);//创建TXT文件
8 fs.Close();
9 }
10
11 StreamReader sR = File.OpenText(txt);//对“打开txt”的操作
12 string nextLine;
13 while ((nextLine = sR.ReadLine()) != null)
14 {
15 path = nextLine;
16 }
17 sR.Close();
1 StreamWriter sw;//将文件名写入txt中
2 sw = File.AppendText(@"C:\DetectFolder\IPV4地址.txt");//将文件名写入运转室text中
3 sw.WriteLine(ip);
4 sw.Close();
使用FileStream需要要注意,同连接SQL数据库一样,每打开一次都在打开另一次前都需要使用close函数将其关闭,否者发生错误(猜测可能跟操作系统中对文件操作道理类似)。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章