How to remove Run As Administration - c#

I have a C# application and I did create a setup project for it.
The tool is working fine and also the setup but I have only one problem which is I need to run the tool as admin every time I want to run it!

Add app.manifest to your exe project and set following
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Related

WinForm DPI issue

There was this issue:
WinForm looks different than in VS designer
I thought I solved with this one:
https://stackoverflow.com/a/13228495/1806838
I used the app.manifest one.
<dpiAware>true</dpiAware>
After that all seemed to be OK. It looks perfect if I debug or if I run the application from the folder where I build it. (app\app\bin\Release\app.exe). But if I copy the executable outside that folder (for e.g. to my Desktop, and I run it then it looks like this (the right one obviously):
It's like the app.manifest is not embedded, but I checked the settings and it seems to be OK. Also, I made sure that in the app properties the correct manifest file is being selected.
Any ideas?
EDIT: So, it seems if the app.exe.manifest is in the same folder then it looks good, otherwise it's not.
EDIT2: I found that I have 2 app.mainfest files. One is app\app\app.manifest and the other one is app\app\Properties\app.manifest. So I deleted both and added a very new one having the DPI setting in it. I still have the same issue. Checked the app.manifest properties and the Build Action is set to None. Shall I change it?
EDIT3: I addedd app.manifest to the Resources and set app -> Properties -> Manifest to be Resources\app.manifest. Still the same.
So, after trying to compile the app.manifest for 3 days, I added another setting to see whether it's going to be applied. This was the run as admin:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
And when I tried to build with that I got an error message stating that it's not compatible with ClickOnce. And this was the point when I realized what a moron I am. After disabling ClickOnce via app -> Properties -> Security -> uncheck Enable ClickOnce security settings everything is just working perfectly. It seems that if it's enabled then it ignores the manifest.
I'm really sorry for the time of people tried to help wasted.

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 force my project in Visual Studio 2013 to always run as Administrator?

I have a WPF project in Visual Studio 2013, this project have two buttons. The first button say Start Service and the second say Stop Service.
When I run my Visual Studio as Administrator, the buttons work. But when I open my Visual Studio without privilages, the InvalidOperationException exception appear.
How to force my project start with privilages when Visual Studio doesn't run as administrator?
I added app.manifest to my project and change for
level="requireAdministrator" uiAccess="false"/>
but it didn't function.
For start or stop my service, I am using ServiceController.
As Torben M. Philippsen mentions in his article:
In Visual Studio 2010 (I guess the same applies to VS2008, but I
haven’t tested it) right click Your project and select “add new item”
Add a application manifest file – the default name will be app.manifest.
Inside the manifest file, change the existing configuration from
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
To
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Save and close the manifest file.
Please note that Your manifest file won’t show up anywhere in your solution. In order to fix that, in solution explorer, click the “show all files” button.
Important: Right click the manifest file and add it to the project – we need that in order to tell VS to use the manifest file when compiling our application.
Right click Your project and select “properties”.
On the application tab, the bottom section, select the manifest file:
manifest file selection
Compile and run the application. If Your UAC settings are enabled, You will be prompted to allow the application to start in elevated mode.
Sometimes it can come in handy to check whether Your application is actually running in elevated mode or not. Maybe You will find this codesnippet usefull:
WindowsPrincipal myPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) == false )
{
//show messagebox - displaying a messange to the user that rights are missing
MessageBox.Show("You need to run the application using the \"run as administrator\" option", "administrator right required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("You are good to go - application running in elevated mode", "Good job" ,MessageBoxButtons.OK, MessageBoxIcon.Information);
}
This is interesting and it seems you need to change permissions of how the project runs, Try doing the following
go to project properties > Security
enable click-once security settings and select Full trust application
More infor in this link
WPF security

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