How C # calls the cmd command prompt running as an administrator

Source: Internet
Author: User

Original address: http://www.yishimei.cn/network/296.html

Since Windows Vista, Win7, and win8/8.1, there are two modes of command line prompts, one is normal user mode, the other is administrator mode, in many cases our program needs to run at the command prompt (Administrator status), but C # Calls are generally ordinary users of the cmd command, although you can right-click-to run the software as an administrator, but we use C # software, can not tell each user to let them all run as an administrator. Today is also the United States network to introduce a simple way to easily implement C # call to run as an administrator command prompt.

The author's environment: win8.1 Professional Edition 64-bit + Visual Studio Ultimate 2013, the development environment is selected. NET framework4.0 (for better compatibility with Win7 and previous versions)

Before the beginning, the author also struggled to search for some methods on the Internet, but the use of more is to Cmd.exe specify the running directory: c:\windows\system32\ Cmd.exe, in fact, someone on the internet has said that this approach does not work, but also the United States network has personally tried, this method does not do, at least in the author's environment did not succeed, then we adopt the following approach.

In fact, to run the cmd command prompt as an administrator, we just need to let C # developed the program itself run as an administrator, we open Visual Studio-Solution Explorer-right-key project name-Properties, pop-up the following window:


The default is "security", we Tick "Enable ClickOnce security Settings" before returning to the Solution Explorer interface, at which point a "app.manifest" file is added under the project name, and we select it to find the code snippet: < requestedExecutionLevel level= "Asinvoker" uiaccess= "false"/>, change it to: <requestedexecutionlevel level= " Requireadministrator "uiaccess= false"/>, after the reset, do not rush to recompile the build, we return to the previous image of the interface, the "Enable ClickOnce security Settings" before the check out and then compile and run, Otherwise, the program will not run the error.

Follow the steps above to set up, C # developed the program will automatically run as an administrator, of course, it inside the command prompt cmd also automatically run as an administrator.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.