HowTo: read your configfile when using no-touch deployment
Earlier I posted about reading a configfile when using no-touch deployment. Several people have contacted me to post the code, so here it is.
private static bool InitializeRemoting()
{
string configFile;
configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
try
{
//try to read the configfile without tricks first
RemotingConfiguration.Configure(configFile);
}
catch (RemotingException)
{
//When 'normal' reading fails, try it our special way.
string tempDir;
WebClient webClient;
//Get the location of the application data folder
tempDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
tempDir += @"\ApplicationName\";
if (!Directory.Exists(tempDir))
{
//Add a folder for our own application
Directory.CreateDirectory(tempDir);
}
try
{
//Download the configfile to the new folder and use it to configure Remoting
webClient = new WebClient();
webClient.DownloadFile(configFile, tempDir + "ApplicationName.exe.config");
RemotingConfiguration.Configure(tempDir + "ApplicationName.exe.config");
}
catch
{
return false;
}
}
return true;
}