Security Settings and SetEnvironmentVariable operation
I have a VB .Net Application that wants to set an environment variable in the System settings. According to the MSDN, the System.Environment.SetEnvironmentVariable can do this, for example:
Code:
System.Environment.SetEnvironmentVariable(_ENV_NAME, value, EnvironmentVariableTarget.Machine)
Problem is, when I execute this code I get a SecurityException. I assume I need to have administrator rights (I am running on Windows 7).
I have done some searching in the forums but cannot find a good example of manipulating system settings that require elevated privileges. I have similar issues with modifying a registry key. The variables and keys need to be present for all users of the machine, so that is why I want to modify these settings. I don't even know where to start. The MSDN is very confusing on this topic.
Re: Security Settings and SetEnvironmentVariable operation
why not give your app administrator privileges?
Project-->Properties-->Application-->View UAC Settings
+ follow the instructions in the app.manifest
Re: Security Settings and SetEnvironmentVariable operation
I am running Studio 2005, and I do not see those settings. I do see a "Security" tab and selected the "Enable ClickOnce Security Settings" and set the option to "This is a full trust application". I still get the error when I run the code to set the global environment variable.
Re: Security Settings and SetEnvironmentVariable operation
sorry, i was assuming you were using vb2008/10
don't think that's possible in vb2005
Re: Security Settings and SetEnvironmentVariable operation
I poked around a little bit, and the security setting tab could be used, I suppose. That controls the Publish wizard (which I was also unaware of) that builds a setup.exe, which I assume somehow builds the security into the installed program.
I have to install several other things with my application, and I use a free-ware install-shield like program called createinstall.
At this point I am stuck. I do not understand the security model in Windows 7 or how to properly program such that I can allow changes to system settings. I guess I may have some re-factoring in my future to not make changes to system settings and registry settings. Some settings seem to be necessary at the system level, i.e. I have license file information that is stored at the local machine level so any user gets the same reg key data and the same license file information.
Ugh. I am going cross-eyed.
Re: Security Settings and SetEnvironmentVariable operation
your executable is automatically built when you run (debug) your project.
Publishing is a different thing entirely, being a way of creating a setup file.
i could be wrong, it might be possible, so don't give up yet
Re: Security Settings and SetEnvironmentVariable operation
Yeah, I am reading up on it a bit. The built-in support in 2005 is limited for VB .net apps. Apparently I have some post-processing steps to do with some tool called mt.exe??? Ugh. I will post a solution WHEN I find one. Never give up, never surrender!
Re: Security Settings and SetEnvironmentVariable operation
Wow. That is all I have to say. This pretty much is a PIA. Good news, I figured it out (yay!). Bad news, there is no support for automating the build process to always add the appropriate manifest information to the executable in Visual Studio 2005. After many reads in the MSDN help, I found information on what was suggested above by .paul.
The steps are:
1.) Manually create a manifest file using a text editor. I did this right in my /bin/Debug folder. Here is what is looks like (myApp.exe is the name of the exe produced in the build step):
file: myApp.exe.manifest.
Code:
<?xml version="1.0" encoding="utf-8"?>
Executable: myApp.exe
Manifest:myApp.exe.manifest
Sample application manifest file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="myApp"
type="win32"/>
<description>A cool application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
the level="requireAdministrator" is key to this whole process. I went big on the access, but you can read up on the other kinds of access levels.
Then after the build I go to DOS and update my path to include the folder that mt.exe is in. Which for me is: "C:\Program Files\Microsoft Visual Studio 8\VC\bin"
Then I navigate to my bin/Debug folder and type:
Code:
mt -manifest myApp.exe.manifest -outputresource:myApp.exe;#1
After I do this, I run my application and Windows puts up the dialog to ask me if it is cool to run my application as an administrator and all is fine. I need figure out where the Publisher setting is so it does not come up as Unknown, but that is the least of my worries at this point.
This has me seriously thinking about using the registry and environment variables. Ugh.