Ejemplo n.º 1
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);
                    }
                }
            }
        }