读书人

服务端StreamReader的方式读取txt数据

发布时间: 2012-05-16 11:12:12 作者: rapoo

服务端StreamReader的方式读取txt数据求指教
我把txt文件当数据源 放服务端
如果以StreamReader的方式读取txt数据,当多个用户同时访问时候 这个会出错吗?


[解决办法]
应该木事吧.

参照msdn

C# code
using System;using System.IO;class Test {    public static void Main()     {        try         {            // Create an instance of StreamReader to read from a file.            // The using statement also closes the StreamReader.            using (StreamReader sr = new StreamReader("TestFile.txt"))             {                String line;                // Read and display lines from the file until the end of                 // the file is reached.                while ((line = sr.ReadLine()) != null)                 {                    Console.WriteLine(line);                }            }        }        catch (Exception e)         {            // Let the user know what went wrong.            Console.WriteLine("The file could not be read:");            Console.WriteLine(e.Message);        }    }}
[解决办法]
读安全,写不安全
[解决办法]
这样就只能用lock了
[解决办法]
读应该没问题,写可能会出错。
[解决办法]
关于文件流 在任何时候基本都只能被一个 进程使用

读也要分情况

例如 你打开 一个文件 不做任何操作

再用程序操作这个文件 有可能也会报错

读书人网 >C#

热点推荐