我现在是这么一个问题。比如说D:\ee.csv文件已经手动打开的前提下我运行到下面的语句
FileStream fw = new FileStream("D:\ee.csv", FileMode.Open, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Unicode);
就会出错,说别的用户正在使用,我没有这个权限。
那我想在这个语句执行之前,先判断下这个文件是不是已经打开了。这个判断语句用那个函数。
那位高手指点下。

解决方案 »

  1.   

    用try 把打开文件的逻辑包起来,catch文件已打开的异常,把文件已打开的处理逻辑放catch块里。不需要判断文件是否已打开。
      

  2.   

    try  catch吧也可以调用API判断,相对繁一点
      

  3.   

    FileStream fs= new FileStream("",FileMode.Create); 
    try 
    {    

    catch(Exception ex) 
    {    
      MessageBox.Show(ex.Message); 

    finally 

      fs.Close();}
    先尝试对文件名更名,若不成功,则说明有文件被独占 
      

  4.   

    8楼说的有道理,FileStream是以独占的方式打开文件的,
    有个API是查文件是否被用的,一时忘了,你去查查呀!
      

  5.   

    FileStream fw = new FileStream("D:\ee.csv", FileMode.Open, System.IO.FileAccess.Write); 
    StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Unicode); 
    FileMode.Open只是Open不能写入
      

  6.   

    用FileStream.CanWrite来判断,代码如下   
        
      using   System;   
      using   System.IO;   
      using   System.Text;   
        
      class   Test     
      {   
                
              public   static   void   Main()     
              {   
                      string   path   =   @"c:\temp\MyTest.txt";   
        
                      //   Ensure   that   the   file   is   readonly.   
                      File.SetAttributes(path,   File.GetAttributes(path)   |   FileAttributes.ReadOnly);   
        
                      //Create   the   file.   
                      using   (FileStream   fs   =   new   FileStream   (path,   FileMode.OpenOrCreate,   FileAccess.Read))     
                      {   
                              if   (fs.CanWrite)     
                              {   
                                      Console.WriteLine("The   stream   for   file   {0}   is   writable.",   path);   
                              }     
                              else     
                              {   
                                      Console.WriteLine("The   stream   for   file   {0}   is   not   writable.",   path);   
                              }   
                      }   
              }   
      }   
      
    Top
      

  7.   

    using System.IO;
    using System.Runtime.InteropServices;[DllImport("kernel32.dll")]
    public static extern IntPtr _lopen(string lpPathName, int iReadWrite);[DllImport("kernel32.dll")]
    public static extern bool CloseHandle(IntPtr hObject);public const int OF_READWRITE = 2;
    public const int OF_SHARE_DENY_NONE = 0x40;
    public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
    private void button1_Click(object sender, EventArgs e)
    {
        string vFileName = @"c:\temp\temp.bmp";
        if (!File.Exists(vFileName))
        {
            MessageBox.Show("文件都不存在,你就不要拿来耍了");
            return;
        }
        IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
        if (vHandle == HFILE_ERROR)
        {
            MessageBox.Show("文件被占用!");
            return;
        }
        CloseHandle(vHandle);
        MessageBox.Show("没有被占用!");
    }
      

  8.   

    将FileStream fw = new FileStream("D:\ee.csv", FileMode.Open, System.IO.FileAccess.Write); 
    改为 
    fs2 = new FileStream(("D:\ee.csv",  FileMode.Open, FileAccess.Write, FileShare.ReadWrite);