简体   繁体   中英

Changing values in Web.config with a Batch file or in .NET code

I have a web.config file on my computer.

There are alot of things i need to change and add in the file. (I am actually working with my SharePoint web.config file)

Can i do this with a Batch file, if so how would i do it. Or how would i do it using VB.NET or C# code?

Any ideas guys?

Edit: i need to create a program to alter a web.config of lets say i web.config laying on my deskop and not the actual web.config of my project

Regards Etienne

You can modify it from C# code, for example:

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");     
AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 

if (appSettingsSection != null) 
{
  appSettingsSection.Settings["foo"].Value = "bar"; 
  config.Save();
}

where foo is the key and bar the value of the key to set, obviously. To remove a value, use Settings.Remove(key);

See the msdn documentation for more information about the OpenWebConfiguration method and more.

The context in which you want to change the file really affects how you should do it. If you're looking at performing changes relatively frequently, but in an administrative domain, then some sort of command-line tool makes sense, and in this case I'd agree with JaredPar that PowerShell would be a valuable tool.

If, on the other hand, you find yourself in a situation where you need to modify the web.config in a more programmatic environment (eg, as part of a setup program), then using programmatic technologies might make more sense. I recently had to do such a thing and Linq to Xml proved very convenient.

For example, to open a document "C:\foo\bar.xml" you could do something like (untested, no convenient build environment at the moment):

XDocument config = XDocument.Load(@"C:\foo\bar.xml");

You could then carry on in the usual fashion with the API . Note that this may be overkill if you're doing an administrative task as opposed to a programmatic task-- there are big, long-term advantages to learning a tool like PowerShell.

Finally, if you're modifying the web.config from within the program that the web.config is being used for, and you aren't doing anything too fancy or dynamic, then using the built-in Settings or ConfigurationManager may be the way to go.

Your best bet might to change it using a MSBuild Script and the MsBuild Community Tasks XML Mass update task

I would personally recommend using PowerShell. This is the next gen command line from Microsoft and it's sits right on top of.Net. It was built to do items like batch edits across large sets of files.

For a change in web.config in a SharePoint environment you have classes specially developed for this task. You only have to search for SPWebConfigModification class .

To load an arbitrary .NET config file

string configLocation = @"C:\myconfigFile.Config";
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();


configFileName = configLocation;
configFileMap.ExeConfigFilename = configFileName;

Configuration configuration= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

Then use Razzie's code to alter the actual config setting

AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings"); 
if (appSettingsSection != null) 
{  
    appSettingsSection.Settings["foo"].Value = "bar";   
    configuration.Save();
}

This is what i needed to do.......thanks for all the help!!!

// Read in Xml-file 
        XmlDocument doc = new XmlDocument();
        doc.Load("C:/Web.config");

        //SaveControl tag..........................................................
        XmlNode n = doc.SelectSingleNode("/configuration/SharePoint/SafeControls");

        XmlElement elemWeb = doc.CreateElement("SafeControl");
        elemWeb.SetAttribute("Assembly", "SamrasWebOption4");
        elemWeb.SetAttribute("Namespace", "SamrasWebOption4");
        elemWeb.SetAttribute("TypeName", "*");
        elemWeb.SetAttribute("Safe", "True");

        XmlElement elemSmartPart = doc.CreateElement("SafeControl");
        elemSmartPart.SetAttribute("Assembly", "Machine_Totals");
        elemSmartPart.SetAttribute("Namespace", "Machine_Totals");
        elemSmartPart.SetAttribute("TypeName", "*");
        elemSmartPart.SetAttribute("Safe", "True");

        //Appending the Nodes......................................................
        n.AppendChild(elemWeb);
        n.AppendChild(elemSmartPart);

        //Saving the document......................................................
        doc.Save("C:/Web.config");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM