当前位置:编程学习 > C#/ASP.NET >>

文件读写

--------------------编程问答-------------------- 写完后有没有调用close?
Object a=new Object();是局部变量?局部变量没有用的 --------------------编程问答-------------------- 写完之后记得关闭 

释放资源  --------------------编程问答-------------------- static string txtPath = @"a.txt";

System.IO.StreamWriter sw = new System.IO.StreamWriter(txtPath, true);

public void Write()
{
      Thread thread = new Thread(new ThreadStart(clays));
            thread.Start();///
            for (int i = 0; i < 10; i++)
            {
                //向a.txt写入数据
                sw.WriteLine("write");
            }
        }


        private void clays()
        {
            for (int i = 0; i < 10; i++)
            {
                //向a.txt写入数据
                sw.WriteLine("clays");
            }
            sw.Flush();

        }

        static void Main(string[] args)
        {
            Program p = new Program();
            try
            {
                p.Write();
                p.sw.Flush();        
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("成功");
                p.sw.Close();
            }
            Console.Read();
        }
    } --------------------编程问答-------------------- 文件打开的时候,必须设置权限,默认是独占的,

你打开文件的构造函数有重载,可以设置共享模式.参考文件打开类的构造函数 --------------------编程问答-------------------- 记得关闭流 --------------------编程问答-------------------- using (FileStream fso = new FileStream(Server.MapPath(DownAdress), FileMode.Open,FileAccess.ReadWrite,FileShare.Inheritable))

试试这个,关键是后边这个,我也不记得哪个是可以共享打开的了
FileShare.Inheritable
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,