Question

I have an implementation of the Process.Start method that fails on my Windows 7 Professional 64-bit development machine, while working in our Windows 2008 test environment.

const string CommandDirectory = @"C:\Program Files (x86)\Command";

var process = new Process
{
    StartInfo =
    {
        FileName = string.Format("{1}{0}MyExecutable.exe", Path.DirectorySeparatorChar, CommandDirectory),
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        UserName = userName,
        Password = securePassword,
        Domain = "MYDOMAIN",
    },
};

process.Start();

At process.Start();, I get the following exception on my development machine:

System.ComponentModel.Win32Exception occurred
  HResult=-2147467259
  Message=The directory name is invalid
  Source=System
  ErrorCode=-2147467259
  NativeErrorCode=267
  StackTrace:
       at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
  InnerException: 

I have tried a few things:

  1. I have ensured that the user account I assign to the process has full rights on the path where the executable lies, as well as the %systemroot% location. I have also ensured the user has "Impersonate a client after authentication" rights in the Local Security Policy.

  2. I have tried explicitly setting the ProcessStartInfo.WorkingDirectory to CommandDirectory. When I do this, the executable launches but immediately crashes, without explanation.

  3. I've made this function on my development machine by removing the UserName, Password, and Domain properties such that it uses my personal credentials. But this is not practical for deployment. This makes it seem to me that it the problem is related to credentials and permissions.

Can anyone advise what I am doing wrong?

Was it helpful?

Solution

@Julien Lebosquain's comment led me to the answer. I needed to log on locally one time as the user I wanted to impersonate. This created the default user folders.

Presumably, the executable I was calling needed to write to AppData or another subfolder of the user directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top