Exemplo n.º 1
0
        private static void FileEnum(string sPath, string sServer = "")
        {
            int    EntriesRead  = 0;
            int    TotalRead    = 0;
            IntPtr ResumeHandle = IntPtr.Zero;
            IntPtr bufptr       = IntPtr.Zero;
            int    ret;

            IntPtr ptrEntry;

            GCHandle handle = GCHandle.Alloc(sServer, GCHandleType.Pinned);
            IntPtr   ptrServer;

            int totalEntries = 0;
            int idx          = totalEntries;


            do
            {
                Console.WriteLine("Server:  {0}", sServer);
                ret = OpenFiles.NetFileEnum(sServer, sPath, null, 3, ref bufptr, OpenFiles.MAX_PREFERRED_LENGTH, out EntriesRead, out TotalRead, ResumeHandle);
                OpenFiles.SetLastError((uint)ret);

                string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                if (ret != (int)OpenFiles.NERR.ERROR_MORE_DATA && ret != (int)OpenFiles.NERR.NERR_Success)
                {
                    Console.WriteLine("Error:  " + errorMessage);
                    //break;
                    //return;
                }

                for (int i = 0; i < EntriesRead; i++)
                {
                    OpenFiles.FILE_INFO_3 entry = new OpenFiles.FILE_INFO_3();
                    ptrEntry = bufptr + (Marshal.SizeOf(entry) * i);
                    entry    = (OpenFiles.FILE_INFO_3)Marshal.PtrToStructure(ptrEntry, typeof(OpenFiles.FILE_INFO_3));

                    idx = totalEntries + i;
                    //if (i == 0)
                    //{
                    //    Console.WriteLine("#,Locks,\"Server Path\",Permissions,\"User ID\"");
                    //}
                    Console.WriteLine("{0},{1},\"{2}\",\"{3}\",{4}", i + 1, entry.fi3_NumLocks, entry.fi3_PathName, entry.fi3_Permissions, entry.fi3_UserName);


                    FileLockInfo info = new FileLockInfo();
                    info.numberOfLocks = entry.fi3_NumLocks;
                    info.remotePath    = entry.fi3_PathName.ToString();
                    info.permissions   = entry.fi3_Permissions.ToString();
                    info.userName      = entry.fi3_UserName.ToString();
                    info.id            = entry.fi3_Id;

                    listFileInfo.Add(info);
                }
            } while (ret == (int)OpenFiles.NERR.ERROR_MORE_DATA);
        }
Exemplo n.º 2
0
        public static FileLockInfo HoldsFileLock(string directoryPath, string fileName)
        {
            var result = new FileLockInfo(directoryPath, fileName);

            try
            {
                var di = new DirectoryInfo(directoryPath);
                foreach (var file in di.GetFiles())
                {
                    if (file.Name.EqualsIgnoreCase(fileName))
                    {
                        result.IsLocked = IsFileLocked(file.FullName);
                        return(result);
                    }
                }
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {
            }

            return(result);
        }