process.start() throws win32Exception when credentials are given

10.7k views Asked by At

I'm trying to use Process.Start() to start a lnk file. it's fine when credentials are not provided, but throws an exception when I do. here's the sample code:

This works fine

var processStartInfo = new ProcessStartInfo
{
    FileName = @"F:\abc.lnk",
};

using (var process = new Process())
{
    process.StartInfo = processStartInfo;
    process.Start();
}

But this code throws a Win32Exception: 'The specified executable is not a valid application for this OS platform'.

var processStartInfo = new ProcessStartInfo
{
    FileName = @"F:\abc.lnk",
    UserName = userName,
    Password = securePassword,
    Domain = domain,
    UseShellExecute = false,
};

using (var process = new Process())
{
    process.StartInfo = processStartInfo;
    process.Start();
}

My OS is 32bit and the program is too

I'll need those credentials as the file is on a network drive.

Any help will be greatly appreciated!!

2

There are 2 answers

0
Bryan On BEST ANSWER

The docs say "When UseShellExecute is false, you can start only executables with the Process component", so passing it a .lnk file you should expect to fail.

Similar problem here: Run application via shortcut using Process.Start C#

1
go.. On
Process process = new Process();
process.StartInfo.FileName = "F:\abc.lnk";
process.StartInfo.Arguments = "use \\\\computerName\\share password /user:UserName";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
process.Dispose();