Wednesday, July 16, 2008

Open File - Security Warning

Ever get that annoying error when trying to run an app from a server or system with a server os installed? Well, if this is a site that you absolutely trust, you know you will not get any virus's or other unwanted attention, then here is a wonderful way to do it in code.

The precursor to this was that I am still developing my main install application. Since it runs .exe files from my server, I was always getting prompted to run the exe. This makes a unattended installation almost impossible. I tried setting the security zones, put no luck. Finally I found out that if you add the sever to you internet options trusted sites list, you can by-pass this prompt.

These settings are, of course, saved in the Registry, and therefore you can programatically change the setting. And if you are worried about the setting, you can always set it back to nothing before the app closes.

Make sure that you have using Microsoft.Win32; declared
Here goes the code:


//disable the Open File - Security Warning in Windows
private void _disableSecurity()
{

try
{
//Declare Variables
RegistryKey _rKey;
object _kPath;

//Open registryKey and get the value of the Domains Key

_rKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\InternetSettings\ZoneMap");

_kPath = _rKey.GetValue("Domains");

//if _kPath is empty then add your Registry key.
if (_kPath == null)
{

//Create the Top Level Domain Key
_rKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet
Settings\ZoneMap\Domains\Your Top Level
Domain
"
);

//Create the SubDomain key
_rKey =
Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet
Settings\ZoneMap\Domains\TLD\Your SubDomain"
);

//beacause we are adding a server, the String name is file, and the object value is 2
_rKey.SetValue("file", 2);
}
}

//catch any exceptions and display them in a popup message box
catch (Exception ex)
{

//writes the message and stacktrace from the error to a messagebox
MessageBox.Show(ex.Message + ex.StackTrace, "Error writing to Registry");
}
}



Thanks and enjoy.... Chuck


No comments: