Start Process as Administrator, without UseShellExecute? - c#

So here is my issue:
I have start a .bin file, which is just a .exe, renamed to .bin, with administrator privileges.
Here is what I have:
PSI.FileName = "Client.bin";
PSI.WorkingDirectory = Directory.GetCurrentDirectory();
PSI.UseShellExecute = false;
PSI.Verb = "runas";
Process.Start(PSI);
I also have the requireAdministrator set in the manifest.
I have to set UseShellExecute to false, seeing is that is the only way I can find that starts the .bin as a .exe. However, according Here, runas, and the manifest only are used when UseShellExecute is set to true.
Question: How to start a non .exe process, with elevated privileges?

Have a parent process which is a executable with administrative privilege & then launch the ".bin" file from it.

According to this answer this is not possible to run .bin file with elevation directly.
At least you can run your own .exe with runas (and ShellExecute = true) and order it with commandline to run .tmp (without ShellExecute, but the process is already elevated) and die. But this is the "last chance" solution.

Related

Running un-elevated command from an elevated C# exe or vice versa

I have an elevated C# exe file that can run elevated commands using System.Diagnostics.Process just fine, however I need to run a specifically unelevated command - that being subst. I am not sure whether I should (or respectively could) make a secondary .exe file or .bat or something?
There is an alternative, as there is only one command that requires admin privileges to run - that being a WSL mount command. Can I make the C# script unelevated and just elevate that one specific command, or vice versa?
I've tried using runas /trustlevel:0x20000, but that throws an access denied (even though the directory being accessed by the SUBST command is \\WSL$\
As for the alternative solution I've tried the following code:
startInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = $"/c powershell -command wsl --mount \\.\PHYSICALDRIVE1 --partition 2;pause"
startInfo.Verb = "runas";
Process proc = Process.Start(startInfo)
proc.WaitForExit();
That opens up an UAC prompt, opens up an elevated powershell Window, but for some reason doesn't recognize "WSL" as a command, even when using it's absolute path.
Fixing either method would fix the issue.

Install Msi file using c#

I am trying to insert msi file using asp.net application. when i run visual studio in administrators mode it is working fine but when i run it in normal mode it is not working.
I had tried following code:
string installerFilePath;
installerFilePath = #"D:\ActivexPractice\test\test\NewFolder1\setup.msi";
System.Diagnostics.Process installerProcess = System.Diagnostics.Process.Start(installerFilePath, "/q");
can any body guide me on this
how to install it without administrators right
You can use msiexec.exe to run installer. Here is sample code.
Process installerProcess = new Process();
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Arguments = #"/i D:\ActivexPractice\test\test\NewFolder1\setup.msi /q";
processInfo.FileName = "msiexec";
installerProcess.StartInfo = processInfo;
installerProcess.Start();
installerProcess.WaitForExit();
If the MSI requires admin rights to install then it will ask for elevation in a UI install. Your /q is failing because a silent install is really silent and will not prompt for elevation. Note that limited users are not allowed to break security rules simply because they are doing an install.
So in that situation your launching process would need to be elevated, either by running as administrator or giving it a requiresAdministrator manifest so it asks for elevation.
When you fire off the install you need to make sure that your elevated state is used to fire off the install. The simplest way to guarantee this is to just call (p/invoke to...) MsiInstallProduct () directly from your code. The issue with Process.Start is that by default ProcessStartInfo.UseShellExecute is true and your elevated state (if you have one) will not be used to start the install. When the install is launched it needs to be a CreateProcess type of execution rather than a ShellExecute type so that your elevated credentials are used.
static void installMSIs(string path)
{
string[] allFiles = Directory.GetFiles(path, "*.msi");
foreach (string file in allFiles)
{
System.Diagnostics.Process installerProcess =
System.Diagnostics.Process.Start(file, "/q");
while (installerProcess.HasExited == false)
{
installerProcess.WaitForExit();
}
}
}

C# - Executing a exe file which further invokes a msi file

I have a .exe file which checks the system architecture and based on the system architecture it calls the corresponding msi file.
I am trying to run this exe file from C# using the code below
Process process = new Process();
process.StartInfo.FileName = "my.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.WorkingDirectory = "path//to//exe//directory";
Console.WriteLine(process.StartInfo.WorkingDirectory);
process.Start();
process.WaitForExit();
The exe is getting invoked. i can see the application logs and there are no errors in the logs.
But the msi is not getting started or installed.
When I try to run the same exe file manually the msi is installed.
Note: There are other dependency files for this my.exe which are placed in the same directory.
Why am i not able to install the msi from C# program while i am able to do this manually.
I am running the VisualStudios in administrator mode.
You need to execute .exe (and msi) as an administrator.
To ensure that, use:
process.StartInfo.Verb = "runas"
Also, try it removing quiet arguments to see any possible errors.
Is "my.exe" installing your MSI if you call it, isn't it?
I got this resolved after i added Thread.Sleep(). before "process.WaitForExit()"

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

How to run .Bat file in asp.net,IIS

How to start XXX.bat file in IIS? I want to run this file..while i am executing in my development PC its perfectly running. But While I deployed in IIS its working and not executing .bat file
So please guide me how to do...My Code as follows
\\
string exefile = ConfigurationManager.AppSettings["exefile"]; //exefile="D:\\XX\\xxx.bat"
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(exefile);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
\\
I would check security.
The user that "hosts" the IIS proccess does likely not have the rights to run exeute files outside the IIS folder.
Also posting a actual error message would be helpfull. On a side note if its Exepction is of type UnauthorizedAccessException then its very likely what i describe above.
I had this problem and for me it was the location of the bat file.
It had placed the bat file in it's own folder under the solution in Visual Studio but this meant IIS could not access it.
When I moved the bat file to the wwwroot folder everything worked as expected.

Categories