Launch an app running as admin - by passing the filename.sln (UseShellExecute) - c#

I can call Process.Start(filename.sln) and it launches VisualStudio with that solution.
But doing so using ProcessStartInfo with Verb="runas" and I get an exception. Even with UseShellExecute=true.
Is there a way to launch an app running as admin where I pass it the app's data file and don't have the application.exe filename?

Found the answer - when you run as admin you can only give it the executable file, not the app for the program to run. So you have to pass devenv.exe, not filename.sln

ProcessStartInfo processInfo = new ProcessStartInfo(); //new process class
processInfo.Verb = "runas"; //wanna admin rights
processInfo.FileName = sDevPath + "devenv.exe"; //exe file path
processInfo.Arguments = sPrjPath + "mConverter.sln"; //sln file as argument
try
{
Process.Start(processInfo); //try to start
}
catch (Win32Exception)
{
//oops
}

Related

error invoking logoff.exe

I am trying to create an app that can kill a user's session on a terminal server.
I have written the following code:
string host = "terminalServer";
string user = "domain\criso";
string sid = "4";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = #"logoff.exe";
startInfo.Arguments = #"/SERVER:" + host + " " + sid;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit();
// Catch error
if (proc.ExitCode != 0)
{
StreamReader reader = proc.StandardError;
string errorMessage = reader.ReadToEnd();
MessageBox.Show(#"ERROR " + proc.ExitCode.ToString() + ": " + errorMessage);
}
else
StatusLabel.Text = user + #"'s Session terminated";
The code above returned error "Couldn't find the file specified" message when executed. I have tried the combination of path to go to C:\windows\system32\logoff.exe but still get the same error message.
I have also tried to invoke cmd.exe process with following argument:
#"/C logoff /SERVER:" + host + " " + sid
it returned with "'logoff' is unrecognized as internal or external command, opreable program or batch file." and still no luck.
Anyone has ever solved this problem before?
For extra information, I am using windows 7 and the terminal server is windows server 2003 & 2008 r2 (there are multiple servers).
And if I run 'logoff' command directly from command prompt, it works fine killing my session.
I found the solution by including 'logoff.exe' into the project and set 'Copy to output directory' property of 'logoff.exe' to yes or copy if newer, which then I don't need to specify the full path on my Process.Start calling.
What's odd is that when I tried to include 'logoff.exe' into my project, VS file explorer didn't list the 'logoff.exe' under 'C:\windows\system32\' directory, but the executable is there if i get into the directory by windows' regular file explorer.
UPDATE
As pointed out in the comment, it looks like that when the app is trying to look into the system32 folder, it is interrupted by syswow64 layer somehow. Based on the comment I found a switch in the project settings to build the app as 32-bit, I turned it off and the app can now call 'logoff.exe' without any issue. But when I try to add existing file from VS file explorer, it still wouldn't list the complete content of the system32 folder (as it was looking into syswow64 folder instead).

Process.Start() throws an "Access Denied" error

When I execute a process through and try to redirect the output/error, I get the following error:
System.ComponentModel.Win32Exception (0x80004005): Access is denied
at System.Diagnostics.Process.CreatePipe(SafeFileHandle& parentHandle, SafeFileHandle& childHandle, Boolean parentInputs)
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
...
What could be wrong? Here is a repro:
string path = "C:\\batch.cmd";
using (Process proc = new Process())
{
bool pathExists = File.Exists(path);
if(!pathExists) throw new ArgumentException("Path doesnt exist");
proc.StartInfo.FileName = path;
proc.StartInfo.WorkingDirectory = workingDir.FullName;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start(); //Exception thrown here
proc.WaitForExit();
}
No decent reason for this to fail, the code has not yet gotten to a point where it would do anything security-sensitive. This is environmental, something on your machine is interfering. Reboot first, disable anti-malware next. If that doesn't help then use TaskMgr.exe, Processes tab and arbitrarily start killing processes, with some luck you'll hit the evil-doer. Ask questions about getting this machine stable again at superuser.com
You have to make sure that the account that execute your program have the rights to execute the program your trying to launch with the process.start, and that the account have the rights to create a pipe on the system .
HAve you tried to remove the redirectOutput ? If without redirecting the output you dont get the exception means that your user can't create a pipe, so you have to give this right to the user .
This should have the full file path and file name, trying to start a folder will result in this error.
string path = "C:\\test.exe";
proc.StartInfo.FileName = path;
Also does the application have administrative privileges?
Edit: if it is a batch file, it needs to have the extension .bat such as "batch.bat" to be run properly. Also if it is a batch file, it cannot be empty or else it will throw an exception.

Redirect StandardIn when opening a shortcut

Due to the joys of UAC, I need to open an elevated command prompt programmatically and then redirect the standard input so I can use the time command.
I can open the link (a .lnk file) if I use
Process ecp = System.Diagnostics.Process.Start("c:/ecp.lnk");
however, if I use this method, I can't redirect the standardIn.
If I use the StartProcessInformation method (which works wonderfully if you are calling an exe)
ProcessStartInfo processStartInfo = new ProcessStartInfo("c:/ecp.lnk");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
StreamWriter inp = process.StandardInput;
StreamReader oup = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
I get the error message:
The specified executable is not a valid Win32 application.
Can anyone help me create an elevated command prompt which I can capture the standard input of? Or if anyone knows how to programatically escalate a command prompt?
In case no-one comes up with a better idea (pretty please), here is the work around one of the more devious in my office just came up with:
Copy cmd.exe (the link it pointing at this file)
Paste this file into a different directory
Rename the newly pasted file to something different
Set the permissions on this new file to Run As Administrator
You will still get the escalation dialog popping up, but at least you can capture the standardIn of this valid Win32 app!

Process.Start() edmgen

after clicking on button in asp.net application process.start() runs edmgen tool with arguments. And I catch error :
var cs =ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string myArgs="/mode:fullgeneration /c:\""+cs+"\" /project:nwd /entitycontainer:SchoolEntities /namespace:SchoolModel /language:CSharp ";
string filename= GetFrameworkDirectory() + "\\EdmGen.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(filename,myArgs);
startInfo.UseShellExecute = false;
//startInfo.RedirectStandardError = true;
Process myGenProcess = Process.Start(startInfo);
//genInfo.Text = myGenProcess.StandardError.ReadToEnd();
How to fix this?
You need to pass the full path to a folder that you have write access to for the output.
Well the error indicates that you don't have access to "C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\nwd.ssdl".
Check that your process has the necessary permissions on the file and all the folders up the tree.

execute/open a program in c#

Is there a solution/references on how to open or execute certain window programs in C#? For example if i want to open WinZIP or notepad application?
Example on the line of codes are more helpful. But anything are welcomed.
thank you.
You can use the System.Diagnostics.Process.Start method.
Process.Start("notepad.exe");
It will work with files that have associated a default program:
Process.Start(#"C:\path\to\file.zip");
Will open the file with its default application.
And even with URLs to open the browser:
Process.Start("http://stackoverflow.com"); // open with default browser
Agree with #Oliver, ProcessStartInfo gives you a lot of more control over the process, an example:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.Arguments = "file.txt";
startInfo.WorkingDirectory = #"C:\path\to";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
Process process = Process.Start(startInfo);
// Wait 10 seconds for process to finish...
if (process.WaitForExit(10000))
{
// Process terminated in less than 10 seconds.
}
else
{
// Timed out
}

Categories

Resources