Example #1
0
        public static void Main(string[] args)
        {
            string filepath = Path.Combine(typeof(TestProgramWhileTrue.Program).Assembly.Location);
            var factory = new ProcessFactory();

            using (var processInfo = factory.Create(null, filepath, true, ProcessCreationFlags.NewConsole, (IEnvironmentBlock)null, null, new ProcessStartInfo()))
            {
                Thread.Sleep(20000);

                var memoryCounters = processInfo.Process.GetProcessMemoryCounters();
                Console.WriteLine("Memory Counters:");
                Console.WriteLine("----------------");
                Console.WriteLine("- PageFaultCount: {0}", memoryCounters.PageFaultCount);
                Console.WriteLine("- PagefileUsage: {0}", memoryCounters.PagefileUsage);
                Console.WriteLine("- PeakPagefileUsage: {0}", memoryCounters.PeakPagefileUsage);
                Console.WriteLine("- PeakWorkingSetSize: {0}", memoryCounters.PeakWorkingSetSize);
                Console.WriteLine("- PrivateUsage: {0}", memoryCounters.PrivateUsage);
                Console.WriteLine("- QuotaNonPagedPoolUsage: {0}", memoryCounters.QuotaNonPagedPoolUsage);
                Console.WriteLine("- QuotaPagedPoolUsage: {0}", memoryCounters.QuotaPagedPoolUsage);
                Console.WriteLine("- QuotaPeakNonPagedPoolUsage: {0}", memoryCounters.QuotaPeakNonPagedPoolUsage);
                Console.WriteLine("- QuotaPeakPagedPoolUsage: {0}", memoryCounters.QuotaPeakPagedPoolUsage);
                Console.WriteLine("- WorkingSetSize: {0}", memoryCounters.WorkingSetSize);

                processInfo.Process.Terminate(0);

                var processTimes = processInfo.Process.GetProcessTimes();
                Console.WriteLine();
                Console.WriteLine("Process times:");
                Console.WriteLine("--------------");
                Console.WriteLine("- CreationTime: {0}", processTimes.CreationTime);
                Console.WriteLine("- ExitTime: {0}", processTimes.ExitTime);
                Console.WriteLine("- KernelTime: {0}", processTimes.KernelTime);
                Console.WriteLine("- UserTime: {0}", processTimes.UserTime);
            }

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey(intercept: true);
        }
Example #2
0
        public static void Main(string[] args)
        {
            var tokenFactory = new TokenFactory();
            var environmentBlockFactory = new EnvironmentBlockFactory();
            var processFactory = new ProcessFactory();

            const string username = "******";
            const string password = "******";

            GetOrCreateUser(username, password);

            using (var token = tokenFactory.Logon(username, ".", GetSecureString(password)))
            {
                using (var environmentBlockHandle = environmentBlockFactory.Create(token, false))
                {
                    var profileInfo = new ProfileInfo
                    {
                        Size = Marshal.SizeOf(typeof(ProfileInfo)),
                        Username = username,
                        DefaultPath = null,
                    };
                    token.LoadUserProfile(ref profileInfo);

                    IProcessInformation processInformation;
                    var processStartInfo = new ProcessStartInfo
                    {
                        Desktop = string.Empty,
                    };
                    string commandLine = string.Format("\"{0}\"", typeof(TestProgramWhileTrue.Program).Assembly.Location);

                    if (Environment.UserInteractive)
                    {
                        processInformation = processFactory.CreateWithLogin(username, "", password,
                            ProcessLogonFlags.None,
                            null,
                            commandLine,
                            ProcessCreationFlags.NewConsole | ProcessCreationFlags.UnicodeEnvironment,
                            environmentBlockHandle,
                            Environment.CurrentDirectory,
                            processStartInfo);
                    }
                    else
                    {
                        processInformation = processFactory.CreateAsUser(token,
                            null,
                            commandLine,
                            false,
                            ProcessCreationFlags.NewConsole | ProcessCreationFlags.UnicodeEnvironment,
                            environmentBlockHandle,
                            Environment.CurrentDirectory,
                            processStartInfo);
                    }

                    using (processInformation)
                    {
                        Console.WriteLine("Press any key to kill");
                        Console.ReadKey(intercept: true);
                        processInformation.Process.Terminate(0);

                        token.UnloadUserProfile(ref profileInfo);
                        token.DeleteUserProfile();
                        DeleteUser(username);
                    }
                }
            }
        }