/// <summary> /// 检查文件夹是否被占用,返回占用的文件名 /// </summary> /// <param name="directoryInfo">文件夹</param> /// <param name="fileName">返回被占用的文件名</param> /// <returns>文件夹占用返回true,否则返回false</returns> public static bool CheckDirectoryIsUsed(DirectoryInfo directoryInfo, out string fileName) { fileName = string.Empty; if (null == directoryInfo || false == directoryInfo.Exists) { return(false); } FileInfo[] files = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories); IntPtr hWndPtr; foreach (FileInfo file in files) { hWndPtr = APIHelper._lopen(file.FullName, OF_READWRITE | OF_SHARE_DENY_NONE); if (hWndPtr == HFILE_ERROR) { //错误的话,不会占用 fileName = file.FullName; return(true); } APIHelper.CloseHandle(hWndPtr); } return(false); }