ProcessStartInfo fails to start Office apps with other system user

2

Trying to launch Office apps with other user on the system is not working, it is throwing this exception:

System.ComponentModel.Win32Exception (0x80004005): A specified logon session does not exist. It may already have been terminated
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

It is working properly for other win32 apps like Notepad, etc..

This is the code snippet:

using System.Diagnostics;
using System.Security;

class Program
{
static void Main(string[] args)
{
    var processPath = @"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"; // Path to a local office app
    ProcessStartInfo ps = new ProcessStartInfo(processPath)
    {
        UserName = "otherUserInTheSystemName",
        Domain = Environment.MachineName,
        Password = StringToSecureString("password"),
        LoadUserProfile = true,
        UseShellExecute = false,
        WorkingDirectory = @"C:\users\otherUserInTheSystemName"
    };
    Process.Start(ps);
}

private static SecureString StringToSecureString(string value)
{
    var password = new SecureString();
    foreach (char c in value)
    {
        password.AppendChar(c);
    }
    return password;
}
}

Tested under Windows 10 2004 (OS Build 19041.508)

c#
windows
asked on Stack Overflow Sep 16, 2020 by Juanma Lopez • edited Sep 17, 2020 by Juanma Lopez

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0