Esempio n. 1
0
        public static IEnumerable <ProcessInfo> GetLockingProcessInfos(string[] paths, LockManagerFeatures features = default)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if ((features & LockManagerFeatures.UseLowLevelApi) != 0)
                {
                    return(NtDll.GetLockingProcessInfos(paths));
                }

                return(RestartManager.GetLockingProcessInfos(paths));
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return(ProcFileSystem.GetLockingProcessInfos(paths));
            }
            else
            {
                if ((features & LockManagerFeatures.ThrowIfNotSupported) != 0)
                {
                    throw new NotSupportedException("Current OS platform is not supported");
                }

                return(Enumerable.Empty <ProcessInfo>());
            }
        }
Esempio n. 2
0
        public static bool RethrowWithLockingInformation(this Exception ex, params string[] fileNames)
        {
            var ioex = ex as IOException;

            if (ioex != null && ioex.IsFileLocked())
            {
                // It is a race to get the lockers, while they are still there. So do this as early as possible.
                var lockers = RestartManager.GetLockingProcessInfos(fileNames).ToList();

                if (lockers.Any())
                {
                    const int max = 10;
                    var       sb  = new StringBuilder();
                    sb.Append(ex.Message);
                    sb.Append(" ");
                    ProcessInfo.Format(sb, lockers, fileNames, max);

                    // We either have a ctor that allows us to set HResult or InnerException, but not both.
                    // But we want both, so we need to use reflection anyway. Since there is an internal
                    // method "Exception.SetErrorCode(int)" but no equivalent for InnerException, we use
                    // the ctor that allows us to set the InnerException and set HResult via reflection.
                    var exception = new IOException(sb.ToString(), ex);
                    if (s_setErrorCodeMethod.Value != null)
                    {
                        s_setErrorCodeMethod.Value.Invoke(exception, new object[] { ex.HResult });
                    }

                    throw exception;
                }
            }
            return(false);
        }
Esempio n. 3
0
        private static int Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.Error.WriteLine("Usage: {0} FILE [FILE ...]",
                                            typeof(Program).Assembly.GetName().Name);
                }

                var infos = RestartManager.GetLockingProcessInfos(args);
                if (!infos.Any())
                {
                    Console.WriteLine("No locking processes found.");
                    return(0);
                }

                bool first = true;
                foreach (var p in infos)
                {
                    if (!first)
                    {
                        Console.WriteLine("----------------------------------------------------");
                    }

                    Console.WriteLine("Process ID        : {0}", p.ProcessId);
                    Console.WriteLine("Process Start Time: {0}", p.StartTime.ToString("F"));
                    Console.WriteLine("Process File Path : {0}", p.FilePath);
                    Console.WriteLine("Process User Name : {0}", p.UserName);
                    Console.WriteLine("Application Type  : {0}", p.ApplicationType);
                    Console.WriteLine("Application Status: {0}", p.ApplicationStatus);
                    Console.WriteLine("Application Name  : {0}", p.ApplicationName);
                    if (p.ApplicationType == ApplicationType.Service)
                    {
                        Console.WriteLine("Service Name      : {0}", p.ServiceShortName);
                    }
                    Console.WriteLine("TS Session ID     : {0}", p.TerminalServicesSessionId);
                    first = false;
                }
            }
            catch (Win32Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                return(ex.ErrorCode);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                return(ex.HResult);
            }
            return(0);
        }