日期:2014-05-18  浏览次数:20817 次

服务端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了
------解决方案--------------------
读应该没问题,写可能会出错。
------解决方案--------------------
关于文件流 在任何时候基本都只能被一个 进程使用

读也要分情况

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

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