Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:07:39+00:00 2026-05-28T14:07:39+00:00

I have a C# Program that is supposed to be multi-OS compatible. It requires

  • 0

I have a C# Program that is supposed to be multi-OS compatible. It requires access to create a directory and get WMI Data, but that is only available if the program is ran as an Administrator. Otherwise, it fails.

Is there any command I can use to not run the program if it does not detect itself as being ran as an administrator? I tried adding a app.manifest and using “requireAdministrator”, it prompts for login, but that appears to only work on Windows 7 and Vista, not XP.

Example:

 if (isAdmin==0)
 Console.WriteLine("Please run this as an administrator");
 exit;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:07:40+00:00Added an answer on May 28, 2026 at 2:07 pm

    Check if the User Is an Admin

    public static bool IsAdministrator()
    {
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
    
        return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    

    Restart the app if not an admin

    public static bool RestartAsAdministrator(string filePath, string fileName, string errorCaption)
    {
        Process process = null;
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.FileName = Path.Combine(filePath, fileName);
    
        if (Environment.OSVersion.Version.Major >= 5) //5 is XP and 6 is Vista and 7
            processStartInfo.Verb = "runas";
    
        processStartInfo.Arguments = "";
        processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
        processStartInfo.UseShellExecute = true;
    
        try
        {
            process = Process.Start(processStartInfo);
        }
    
        catch (Exception)
        {
            MessageBox.Show("Couldn't start as admin.\nPlease try manually by Right Clicking on " + Path.GetFileNameWithoutExtension(fileName) + " and selecting \"Run as administrator\"",
                                errorCaption + " Error", MessageBoxButton.OK, MessageBoxImage.Error);
    
            return false;
        }
    
        finally
        {
            if (process != null)
                process.Dispose();
        }
    
        return true;
    }
    

    Application Manifest File (Used to automatically Run as Admin)

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that is supposed to interact with a web server and
I have a program for the iPhone that is supposed to be doing intelligent
I have a program that is supposed to send a file to a web
I have a button in my program that supposed to be clicked after a
Greetings all, I’m working on a C# program that requires being able to get
I have to create a program that finds all the possible ways of filling
I have a C# program that works correctly on xp and vista but it
Supposing I have a File f that represents a directory, then f.delete() will only
I have a program that is supposed to clear the screen and print my
I have simple serial port program that is supposed to read the serial port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.