C#判断文件是否正在被使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Web.Mvc;
namespace HotelInventory.Web.Models.Repository
{
    public static class FileControlRepository1
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);//调用windowsdll
        
        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);//调用windowsdll
        public const int OF_READWRITE = 2;//这些参数是不可少的,当然也可以不声明,直接将值赋值给对应的函数,这里只是生明变量将其存起来而已
        public const int OF_SHARE_DENY_NONE = 0x40;//这些参数是不可少的,当然也可以不声明,直接将值赋值给对应的函数,这里只是生明变量将其存起来而已
        public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);  
        public static int IsFileOpen(string path)
        {
            string vFileName = path;
            if (!File.Exists(vFileName))
            {
             return 0;//文件不存在
            }
            IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);//windows Api上面有定义扩展方法
            if (vHandle == HFILE_ERROR)
            {
             return 2;//文件被占用
            }
            CloseHandle(vHandle);//windows Api上面有定义扩展方法
            return 1;//文件存在且没被占用
        }
    }
}		 
             
	
         
                                     
                                    