Check out Runas
runas /user:somedomain\someuser "cmd /c start c:\somedocument.pdf"
It is located at C:\Windows\System32\runas.exe
To open this from a C# application, you could use Process.Start
, with the appropriate flags.
Edit
Well, you can skip the use of Runas
entirely, since Process.Start
can do the same job, and still allow you to specify the password however you like (hard-coded internally, or via the UI).
Simply use cmd.exe /c start <pathToFile>
to launch the file via the shell with the associated program:
string cmdPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System),
"cmd.exe");
string workingDirectory = @"C:\users\public";
string pathToFile = Path.Combine(workingDirectory, "somefile.png");
string arguments = string.Format("/c start {0}", pathToFile);
var password = new SecureString();
foreach (char c in "usersPassword")
password.AppendChar(c);
var processStartInfo = new ProcessStartInfo()
{
FileName = cmdPath,
Arguments = arguments,
WorkingDirectory = workingDirectory,
UserName = "TestUser",
Domain = Environment.MachineName, // Could use domain
Password = password,
UseShellExecute = false,
};
Process.Start(processStartInfo);