Paul Sheriff's Blog for the Real World

This blog is for my ramblings and to share my tips, tricks and advice garnered over 20+ years in the IT industry. I like to focus on topics that affect real-world business application developers.

Finding Machine.Config

OK, this is just a quick little tip. I need to be able to programmatically locate the Machine.Config file. Instead of hard-coding the path where I know it is, I used the new ConfigurationManager class in .NET 2.0 to perform this. Here is the code.

C#
using System.Configuration;

Configuration cnf;

cnf = ConfigurationManager.OpenMachineConfiguration();

MessageBox.Show(cnf.FilePath);


VB.NET
Imports System.Configuration


Dim cnf As Configuration

cnf = ConfigurationManager.OpenMachineConfiguration()

MessageBox.Show(cnf.FilePath)

You will need to add a reference to the System.Configuration.dll to be able to use this, but then you can use File IO or XmlDocument methods to open and affect the Machine.Config.

 

Hope this helps someone out.

Paul

Comments

Eric Newton said:

"...but then you can use File IO or XmlDocument methods to open and affect the Machine.Config"

...which is a daunting thought... and should be treated with even more caution than editing keys in the registry!

# February 27, 2007 4:16 PM

Sergey said:

Thanks

Timely post

# February 28, 2007 6:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)