How to Force My C# Winforms Program Run as Administrator on Any Computer

How to force my C# Winforms program run as administrator on any computer?

You can embed this manifest into your application.

<?xml version="1.0" encoding="utf-8" ?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>

How to force my project in Visual Studio 2013 to always run as Administrator?

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 do I force my .NET application to run as administrator?

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 <requestedExecutionLevel> element to:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

The user gets the UAC prompt when they start the program. Use wisely; their patience can wear out quickly.

c# windows form, When I run as admin, I cant get the current username

You might try WMI and see if this works (if you don't have it referenced there is a Nuget package for it).

string username;

using (var searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem"))
{
using (var collection = searcher.Get())
{
username = collection.Cast<ManagementBaseObject>().First()["UserName"].ToString();
}
}

Make winforms application run on other computer

  1. Yes, your executable will run fine on another computer as long as it has the corresponding .Net package and any referenced assemblies.

  2. No, there is no way to run your application on a machine without .Net installed. .Net is installed out of the box unless you're running WinXP. You can ship .Net installers with your app.

How to run my winform application when computer starts

You can add a shortcut to your application in the startup folder, or through a registry value (Most applications use the HKLM or HKCU\Software\Microsoft\Windows\CurrentVersion\Run key in the registry or put a shortcut in the C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder. There are other options but these are the most popular)

Sample:

Microsoft.Win32;
...

//Startup registry key and value
private static readonly string StartupKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
private static readonly string StartupValue = "MyApplicationName";

...
private static void SetStartup()
{
//Set the application to run at startup
RegistryKey key = Registry.CurrentUser.OpenSubKey (StartupKey, true);
key.SetValue(StartupValue, Application.ExecutablePath.ToString());
}

You can see the results of this code in regedit:
http://i.imgur.com/vYC5PPr.png

The Run key (And alternatively the RunOnce key for running your application once) will run all applications that are in it on startup/when a user logs on.

This is the code I use in my applications, and it works great. You don't need any special installer to do this, you can simply call this method every time your app starts and it will set/update the applications value in the Run key in the registry with the path to the executable.

The startup folder alternative is a little more involved to set up, check out this tutorial for help.



Related Topics



Leave a reply



Submit