How to restart my application in admin rights in C# [duplicate] - c#

This question already has answers here:
Run process as administrator from a non-admin application
(1 answer)
How can you modify an in-use dll?
(1 answer)
Closed 5 years ago.
I realise something that when I restart my application (with code), it does not restart with admin rights. When I start my app by double clicking, it ask me that you want to run it as an admin and I say yes. But after that I need to restart my application programmatically and it has to restart as also admin but it does not.
I use simply this code to restart
Application.Restart();
and I added the following line to my app manifest file
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Edit: I am not looking for using Progress class to restart. can I do it with Application.Restart?
We made a management console and we have plugins and plugins are needed to be updated. Thats why I have to delete the old dlls to upgrade but I receive errors when I try and I believe it is because the program does not have administrator permissions.

Related

c# winforms Application, run a cmd command as system

I have written a program in the last days which automatically changes the proxy server. This runs as a Windows Forms Tray Application on the user level.
Now came a request which caused me some problems. A cmd command "ipconfig /registerdns" should be executed but this requires higher permissions.
I'm now looking for a way to easily execute this command in normal user context. Is there a way to run this one command as a system? Or any other possibility?
Or is there another function to re-register the dns without admin priviledge?
I hope there is a easy way.
Thank you very much!
As i know admin processes can only be created by another admin processes but still you can try adding this parameter to your process's start info
StartInfo.Verb = "runas";
if it doesnt work you set your application to run always as admin by adding app.manifest to your project and editing line 19 as below.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Create a script (.cmd or .bat) containing required command (here: "ipconfig /registerdns"), then flag it as "Run as administrator". Finally, run this script from your app instead of original command.
This should help in flagging as "Run as administrator": How to code a BAT file to always run as admin mode?
Okay so I found no solution for my former problem, because it is not possible to launch a elevated command from a user Application. But I found a powershell command: Register-DnsClient. This command does the same and does not need elevated priviledges. Only downside is it works only on Windows 8.1 and newer. So Windows 7 is not supported.

How to Properly create Install Shield Setup in Visual Studio

Seems like a simple question but I am facing some weird problems.
I am using Visual Studio 2015, Enterprise Edition and Install Shield to create setup of my software, this is my first time making a general purpose software. Everything goes according to plan but I get these 2 problems.
Problems are:
Shortcuts don't work
Application doesn't launch until I run from root directory as admin
Problem 1:
I create the setup and do everything asked, I put the primary output into setup, which by the way contains 2 files. the .exe file and a .config file and I choose the primary output in "add" button to create a shortcut but when I install that setup, the Desktop shortcut doesn't work, in no way.
Problem 2:When I simply double click on the software's main .exe file, it doesn't run. No response but when I run it as Admin, it responses and opens. The problem is weird cause this doesn't happen in debug or the release files of the software.
Is there something I am doing wrong? maybe the way I insert the primary output?
This doesn't appear to be an installation problem. Your statement "..when I run it as Admin, it responses and opens" means that your program requires admin privilegs to run correctly, and running it from a shortcut does not automatically run it as administrator. The usual way to say that a program needs elevation when it runs is to give it an elevation manifest as here:
https://msdn.microsoft.com/en-us/library/bb756929.aspx
with requireAdministrator. The program will then show the standard dialog requesting elevation.
The most likely reason for your program doing nothing when it fails is that it silently crashes and goes away, and that's probably because your code isn't explicitly making sure that everything you do is actually working. For example, if you try to create/modify a file in Program Files (and you're not elevated) it will fail, and your code should check that access was denied.

Can I set the properties of an exe file via C#?

I am using Unity to build my game. I need the application to always run as administrator.
When I build my game, I right click on the exe that is produced and set 'Run this program as an administrator' on in the Compatibility > Settings section of the exe properties.
I can write an editor script (C#) that'll execute after a build has completed. So I was wondering if I could automate this step so that I do not forget to do it every time I build?
I'm not sure if this solves your issue.
But you could add an "Application Manifest File" and configure:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
With this configuration the user gets always a UAC promt.
Do not have a ready made solution but i can give you an idea:
Have a look at this, using this way you can check if your application is running as an administrator, if no, it starts another process with administrative privilege and you can later call:
Application.Quit();
to terminate the current instance.
Other than this you must have to wrap this code under platform dependent compilation.
I know it is not a best solution, but can fix this issue.
Hope it helps!

How to run application as administrator in debug with Visual Studio?

I have a c# application where I have to have read/write access to the root of the C drive. I realize I can compile the code and run the executable as administrator and it works. But I need to debug it and I am unsure as to how one would start the app within Visual Studio.
I have tried adding:
<requestedExecutionLevel level="asInvoker" uiAccess="true" />
to my manifest but I still get access denied error.
Here is the line of code that fails:
MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(#"c:\somemapnamefile.data", System.IO.FileMode.OpenOrCreate, "somemapname", 1000);
For now I have a work around but I'd like to know for the future.
Just run visual studio itself as an administrator. Any program you debug from there will also be run as an administrator.
VS must be run with admin right. however, a more elegant way is in the requiredExecutionLevel in manifest should set to 'requireAdministrator'.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
When you open the project and try to debug, the VS2012 will warn about the admin right and restart itself to admin right. And also the exe file will be marked as requiring admin right at the first place therefore when deploy you don't need to configure admin right requirement in file properties.
You can also set this administrator option automatically:
To answer the question in your title, you can just select Run as Administrator from the context menu when starting VS.
Now the checked answer will not working.
You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level. Set this to requireAdminstrator.
This will cause the default generated manifest to include the requestedExecutionlevel that you need, so that your users will be prompted automatically to elevate their privileges if they are not already elevated.
The "This task requires the application to have elevated permissions" error occurs because of The current user didn’t have a sufficient privilege to open Visual Studio.
As a temporary solution
You can overcome this issue by right-clicking on visual studio and select run as administrator at every time you intend to open it
As a permanent solution,
You can check the compatibility troubleshooting
Right, Click on Visual Studio > select Troubleshoot compatibility.
Select Troubleshoot Program.
Check The program requires additional permissions.
Click on Test the program.
Wait for a moment until the program launch. Click Next.
Select Yes, save these settings for this program.
For the detail steps with images, please check Visual Studio requires the application to have elevated permissions

Windows Forms - C# - ask for administator privilege [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to force C# App to run as administrator on Windows 7
I am building a WinForms C# appliction, and I need it to ask for administrator privileges so I can copy and open directories in C:\.
Is this possible?
The code I am going to use (if any one needs) is this:
if (!Directory.Exists("C:\\smm"))
{
Directory.Create("gg");
}
Or something like that, but I am sure I need administrator privilege.
Anyone know how I can do this?
You need to enable ClickOnce security settings in your project, then change the application manifest to require administrator privileges. This will cause Windows to show a UAC elevation prompt when the process starts, so the user can escalate your program to admin.
To enable ClickOnce, go into your project's properties, select the Security tab on the left, then check the "Enable ClickOnce Security Settings" box. Then go into the project's "Properties" directory, and open up the app.manifest file. In that file, there's a line that sets the required privileges:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
You can make it require administrator privileges like this:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
That'll make it require admin when the process starts.

Categories