Elevating a process to run as admin doesn't work. If I run that application from an elevated command prompt it runs fine. But my code below doesn't.
Process setupws = new Process();
setupws.StartInfo.FileName = #"setupws.exe";
setupws.StartInfo.Verb = "runas";
setupws.StartInfo.UseShellExecute = true;
setupws.Start();
setupws.WaitForExit();
The setupws.exe file runs fine, just not as an admin.
What am I doing wrong?
Thanks
PS. I've also used highestAvailable and requireAdministrator in my app.manifest file
You need to mark your installer as requestedExecutionLevel level=requireAdministrator in the manifest, see Create and Embed an Application Manifest (UAC).
PS. The requireAdministrator should be in the setupws.exe's manifest.
Have you ran your app as admin and tried it that way? You could do something like:
using System.Security.Permissions;
var mine = new EnvironmentPermission(PermissionState.Unrestricted);
mine.AddPathList(EnvironmentPermissionAccess.AllAccess, Environment.CurrentDirectory);
Related
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();
}
}
}
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()"
When I run dism /Online /Disable-Feature:Microsoft-Hyper-V-Allcommand from command prompt, It works fine.
But same I want to do from C# code. It does not work, Process exists with error code 11.
Process proc = new Process();
proc.StartInfo.FileName = "dism.exe";
proc.StartInfo.Arguments = "/Online /Disable-Feature:Microsoft-Hyper-V-All";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
Basically I want to run the given command from C# code (with UAC) as it works with command prompt.
You can't. That is, you as a programmer, don't get to decide whether or not your code runs with administrative rights. However, you can inform the user that your code requires administrative rights and then ask to be granted those rights. How to do that is covered here.
try this method,
You'll want to modify the manifest that gets embedded in the program. This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". Change the element to:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
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.
I'm trying to build an updater using msiexec to uninstall a program, then install the newer version.
Here's my code:
command = "/x{[uninstall string here]}";
command += "/qn+ /Le c:\\test\\msilog.txt";
ProcessStartInfo psi = new ProcessStartInfo("msiexec");
psi.Arguments = command;
//psi.UseShellExecute = true;
//psi.Verb = "runas";
Process.Start(psi);
I have
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
set in my manifest, and that is set as the Application's manifest.
When I run this I get the popup box that says "AppSetup failed" and the log file says
Error 1730. You must be an Administrator to remove this application. To remove this application, you can log on as an Administrator, or contact your technical support group for assistance.
IF, however, I run cmd as administrator, and type in
msiexec /x{[uninstall string here]} /qn+ /Le c:\\test\\msilog.txt
It works fine and dandy.
What am I missing here?
(I've also tried uncommenting those two lines above, as that was one way I found to run as Administrator, but it then pops up the UAC dialog before trying to execute, even though /qn is set.)
To elevate a process you need to have the user approve it. If every process could elevate itself without user interaction it would somewhat defeat the purpose of elevation.
I don't know your full scenario, but if you can managed to execute your updater from the Local Service account then this should work without user interaction. A few ways that come to mind are by installing a windows service, Run/RunOnce key of local service account, or using psexec with -s. Of course to accomplish this, you need to have the right permissions yourself on the client machine.
Good luck.
*One more thing:
If you haven't looked into this yet, you can use Windows Installer to update your installation and do not need to write your own "updater." There are 3 different types of updates (small update, minor upgrade, and major upgrade): http://msdn.microsoft.com/en-us/library/aa370579(VS.85).aspx
A major upgrade uninstalls the previous version and installs the newer version which is most similar to what you've described, though in most cases small updates and minor upgrades are more fitting.