none
Environment Variables Administrator Privileges! RRS feed

  • Question

  • Hi,

    I have an application and I want to save the store ID and POS ID in the environment variables (System variables)

    I want to ask, can I set the variables from C# or application needs to have administrator privileges?


    Thanks,
    jassim
    Thursday, June 23, 2016 11:27 AM

Answers

  • Hi Jassim,

    >>"then how can I set the admin privileges to my application?"

    You could create and embed an Application Manifest file for your application to do it. Steps below are for your reference.

    1. Right click your project->Add -> New Item -> Choose "Application Manifest File".

    2. Change the <requestedExecutionLevel> element as following.

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

    When user open your application, it will prompt a User Account Control dialog to let user choose run as Administrator.

    You could use following code to determine whether the current user is Administrator or not.

    public bool IsUserAdministrator()
    {
        bool isAdmin;
        try
        {
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch (UnauthorizedAccessException ex)
        {
            isAdmin = false;
        }
        catch (Exception ex)
        {
            isAdmin = false;
        }
        return isAdmin;
    }

    If User Account Control is disabled by user, the User Account Control dialog will not work, you need to show a message for user. The message could remindthe user to manually enable User Account Control. For how to enable User Account Control, link below is for your reference.
    http://windows.microsoft.com/en-sg/windows/turn-user-account-control-on-off#1TC=windows-7

    In addition, you need to test requireAdministrator by click the .exe file of your application. It will not work when you press F5 to debug your application.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 24, 2016 2:20 AM
    Moderator

All replies

  • Depends on your requirements what Env variables are both: User and System

    If System the AppID running the application would need admin privileges else user environment variables wouldn't need it.Assuming its a Windows application and not Web Application I would think you want to use the POSID for a different user so you need to set System Environment varaible which would need Admin privildges.

    However to me trying to use environment variables for a desktop/windows application wouldnt make much sense


    Abhinav http://bishtabhinav.wordpress.com/

    Thursday, June 23, 2016 4:18 PM
  • then how can I set the admin privileges to my application?
    Thursday, June 23, 2016 5:05 PM
  • Hi Jassim,

    >>"then how can I set the admin privileges to my application?"

    You could create and embed an Application Manifest file for your application to do it. Steps below are for your reference.

    1. Right click your project->Add -> New Item -> Choose "Application Manifest File".

    2. Change the <requestedExecutionLevel> element as following.

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

    When user open your application, it will prompt a User Account Control dialog to let user choose run as Administrator.

    You could use following code to determine whether the current user is Administrator or not.

    public bool IsUserAdministrator()
    {
        bool isAdmin;
        try
        {
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch (UnauthorizedAccessException ex)
        {
            isAdmin = false;
        }
        catch (Exception ex)
        {
            isAdmin = false;
        }
        return isAdmin;
    }

    If User Account Control is disabled by user, the User Account Control dialog will not work, you need to show a message for user. The message could remindthe user to manually enable User Account Control. For how to enable User Account Control, link below is for your reference.
    http://windows.microsoft.com/en-sg/windows/turn-user-account-control-on-off#1TC=windows-7

    In addition, you need to test requireAdministrator by click the .exe file of your application. It will not work when you press F5 to debug your application.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 24, 2016 2:20 AM
    Moderator
  • Hi jassim,

    How about your issue now?

    Is it resolved?

    If you have then please mark a post or posts as answer, vote those you liked or helped.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 4, 2016 7:20 AM
    Moderator