Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to deal with environment variables and non admin users

How to deal with environment variables and non admin users

Scheduled Pinned Locked Moved C#
windows-adminjsontutorialworkspace
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bfis108137
    wrote on last edited by
    #1

    I have a program that needs to run at startup for each user on the pc and it reads a file that was made using binary serialization. The file just sits in the program folder but the path that comes up (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken. So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied. I tried setting an environment variable, but it requires admin privileges to access the variables. I must be going about this the wrong way. Could someone please point me in the right direction.

    L 1 Reply Last reply
    0
    • B bfis108137

      I have a program that needs to run at startup for each user on the pc and it reads a file that was made using binary serialization. The file just sits in the program folder but the path that comes up (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken. So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied. I tried setting an environment variable, but it requires admin privileges to access the variables. I must be going about this the wrong way. Could someone please point me in the right direction.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      bfis108137 wrote:

      The file just sits in the program folder

      Can you give us the full path? Also from the serialized file you're reading?

      bfis108137 wrote:

      (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken

      From MSDN[^]: "Gets the path for the executable file that started the application, not including the executable name."

      bfis108137 wrote:

      So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied.

      So the app is started from the registry ("Windows/CurrentVersion/Run"), and not from, say, the start-menu folder "startup" for all users? Is it pointing to the full path?

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      B 1 Reply Last reply
      0
      • L Lost User

        bfis108137 wrote:

        The file just sits in the program folder

        Can you give us the full path? Also from the serialized file you're reading?

        bfis108137 wrote:

        (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken

        From MSDN[^]: "Gets the path for the executable file that started the application, not including the executable name."

        bfis108137 wrote:

        So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied.

        So the app is started from the registry ("Windows/CurrentVersion/Run"), and not from, say, the start-menu folder "startup" for all users? Is it pointing to the full path?

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        B Offline
        B Offline
        bfis108137
        wrote on last edited by
        #3

        The path I tried was in 2 places. First I made a folder in the c root. Then I thought it had to do with permissions so I used the public folder. This is c:\users\public on win7/vista and it's c:\documents and settings\All Users in xp. Like I said I used Application.StartupPath and it works but only when I start it myself. When it runs automatically at startup, "Application.StartupPath" points to c:\users\current user. With all that being said I found a solution. I add an environment variable which does require elevation but it's a one time thing when the app is first installed which i use a separate app to do(which i call from the main app) so as to not require the higher privilege all the time. That much I had working before. Where I got stuck was trying to retrieve the variable. It would require elevation as well which was unacceptable. However I found a solution. I found this on another site but since I know how some sites are with links I am not going to post the link. Here is the code I used to retrieve an environment variable without elevation.

        string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\";
        string path = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("variablename", "", RegistryValueOptions.DoNotExpandEnvironmentNames);

        D 1 Reply Last reply
        0
        • B bfis108137

          The path I tried was in 2 places. First I made a folder in the c root. Then I thought it had to do with permissions so I used the public folder. This is c:\users\public on win7/vista and it's c:\documents and settings\All Users in xp. Like I said I used Application.StartupPath and it works but only when I start it myself. When it runs automatically at startup, "Application.StartupPath" points to c:\users\current user. With all that being said I found a solution. I add an environment variable which does require elevation but it's a one time thing when the app is first installed which i use a separate app to do(which i call from the main app) so as to not require the higher privilege all the time. That much I had working before. Where I got stuck was trying to retrieve the variable. It would require elevation as well which was unacceptable. However I found a solution. I found this on another site but since I know how some sites are with links I am not going to post the link. Here is the code I used to retrieve an environment variable without elevation.

          string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\";
          string path = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("variablename", "", RegistryValueOptions.DoNotExpandEnvironmentNames);

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          bfis108137 wrote:

          Like I said I used Application.StartupPath and it works but only when I start it myself. When it runs automatically at startup, "Application.StartupPath" points to c:\users\current user.

          No it doesn't. Not unless your .EXE is in that path and was launched from there. I'll guarantee it.

          bfis108137 wrote:

          Where I got stuck was trying to retrieve the variable. It would require elevation as well which was unacceptable.

          You do not need elevated priv's to get an environment variable. You only need them to create a system-wide, or machine, environment variable. Everything you're telling us is backwards from how it actually works. There's something you're not telling us about this code. Since we can't see it, we can't tell you why it's not working.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          B 1 Reply Last reply
          0
          • D Dave Kreskowiak

            bfis108137 wrote:

            Like I said I used Application.StartupPath and it works but only when I start it myself. When it runs automatically at startup, "Application.StartupPath" points to c:\users\current user.

            No it doesn't. Not unless your .EXE is in that path and was launched from there. I'll guarantee it.

            bfis108137 wrote:

            Where I got stuck was trying to retrieve the variable. It would require elevation as well which was unacceptable.

            You do not need elevated priv's to get an environment variable. You only need them to create a system-wide, or machine, environment variable. Everything you're telling us is backwards from how it actually works. There's something you're not telling us about this code. Since we can't see it, we can't tell you why it's not working.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            B Offline
            B Offline
            bfis108137
            wrote on last edited by
            #5

            Well it was a machine environment variable since the path was going to be the same across all users. And you are right Application.StartupPath does work correctly but here is where the problems started. I was using a dll. I wasn't using Application.StartupPath. I was just trying to get information out of a file so I wasn't even using a path. I was just doing something like string someString = File.ReadAllText("FileName.txt"); When it would run normally (not at startup) then it would read and write to the folder where the app was. If it was run at startup however, it would read or write to the user's folder. I misspoke myself about the Application.StartupPath. I have been doing a lot of debugging and apparently that wasn't what happened but I assure you the issue I just presented did happen. The file with a dll is in c:\test. Here is the code. Now the first time it's run as expected there is a file called error.txt which says "Could not find file 'C:\test\test.txt'." The path.txt file and the test.txt files are as expected. However when it runs at startup, the time in the test.txt file doesn't change and when I look in the user folder, I see a new error.txt file along with a test.txt file. The error.txt file says "Could not find file 'C:\Documents and Settings\User\test.txt'." and the time in the test.txt file is more up to date. So again I misspoke before and I assure you it was accidental but and I have found a way to solve my problem but this was my situation. I hope you accept my apology. A button and form load with the following code.

            private void Form1_Load(object sender, EventArgs e)
            {
            TestClass tc = new TestClass();
            tc.ReadFile();
            tc.WriteFile();
            }

                private void button1\_Click(object sender, EventArgs e)
                {
                    string path = Application.StartupPath + @"\\CurrentFolderTest.exe";
                    RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run", true);
                    File.WriteAllText("path.txt", Application.StartupPath);
                    key.SetValue("foldertest", path);
                }
            

            Here is the code from the dll

            public void WriteFile()
            {
            File.WriteAllText("test.txt", DateTime.Now.ToLongTimeString() + DateTime.Now.Millisecond.ToString());
            }
            public void ReadFile()
            {
            try
            {
            string test = File.ReadAllText("test.txt");

            D 1 Reply Last reply
            0
            • B bfis108137

              Well it was a machine environment variable since the path was going to be the same across all users. And you are right Application.StartupPath does work correctly but here is where the problems started. I was using a dll. I wasn't using Application.StartupPath. I was just trying to get information out of a file so I wasn't even using a path. I was just doing something like string someString = File.ReadAllText("FileName.txt"); When it would run normally (not at startup) then it would read and write to the folder where the app was. If it was run at startup however, it would read or write to the user's folder. I misspoke myself about the Application.StartupPath. I have been doing a lot of debugging and apparently that wasn't what happened but I assure you the issue I just presented did happen. The file with a dll is in c:\test. Here is the code. Now the first time it's run as expected there is a file called error.txt which says "Could not find file 'C:\test\test.txt'." The path.txt file and the test.txt files are as expected. However when it runs at startup, the time in the test.txt file doesn't change and when I look in the user folder, I see a new error.txt file along with a test.txt file. The error.txt file says "Could not find file 'C:\Documents and Settings\User\test.txt'." and the time in the test.txt file is more up to date. So again I misspoke before and I assure you it was accidental but and I have found a way to solve my problem but this was my situation. I hope you accept my apology. A button and form load with the following code.

              private void Form1_Load(object sender, EventArgs e)
              {
              TestClass tc = new TestClass();
              tc.ReadFile();
              tc.WriteFile();
              }

                  private void button1\_Click(object sender, EventArgs e)
                  {
                      string path = Application.StartupPath + @"\\CurrentFolderTest.exe";
                      RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run", true);
                      File.WriteAllText("path.txt", Application.StartupPath);
                      key.SetValue("foldertest", path);
                  }
              

              Here is the code from the dll

              public void WriteFile()
              {
              File.WriteAllText("test.txt", DateTime.Now.ToLongTimeString() + DateTime.Now.Millisecond.ToString());
              }
              public void ReadFile()
              {
              try
              {
              string test = File.ReadAllText("test.txt");

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              That's better. Two problems. First, normal users can't write to the Localmachine registry hive. Writing this key is something you would do at install time, not run time. Second, whenever you read or write files, it is best practice to always build a fully qualified path to the file using a well known folder, like CommonAppData. Use Environment.GetFolderPath get the base folder path and the use Path.Combine to build your fully qualified file path from it. Like you already found, the base folder paths between Windows XP and 7 are different. They can even change between 32 and 64 bit versions of Windows.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups