[英]Changing values in Web.config with a Batch file or in .NET code
我的電腦上有一個 web.config 文件。
我需要在文件中更改和添加很多東西。 (我實際上正在使用我的 SharePoint web.config 文件)
我可以用批處理文件執行此操作嗎,如果可以,我將如何執行。 或者我將如何使用 VB.NET 或 C# 代碼來做到這一點?
有什么想法嗎?
編輯:我需要創建一個程序來更改 web.config 假設我 web.config 放在我的桌面上,而不是我項目的實際 web.config
問候艾蒂安
您可以從 C# 代碼修改它,例如:
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
if (appSettingsSection != null)
{
appSettingsSection.Settings["foo"].Value = "bar";
config.Save();
}
顯然,其中 foo 是鍵,而 bar 是要設置的鍵的值。 要刪除一個值,請使用 Settings.Remove(key);
有關 OpenWebConfiguration 方法等的更多信息,請參閱msdn 文檔。
您要更改文件的上下文確實會影響您應該如何做。 如果您正在考慮相對頻繁地執行更改,但在管理域中,那么某種命令行工具是有意義的,在這種情況下,我同意 JaredPar 的觀點,即PowerShell將是一個有價值的工具。
另一方面,如果您發現自己需要在更加程序化的環境(例如,作為安裝程序的一部分)中修改 web.config,那么使用程序化技術可能更有意義。 我最近不得不做這樣的事情, Linq 到 Xml證明非常方便。
例如,要打開文檔“C:\foo\bar.xml”,您可以執行以下操作(未經測試,目前沒有方便的構建環境):
XDocument config = XDocument.Load(@"C:\foo\bar.xml");
然后,您可以按照通常的方式使用API 。 請注意,如果您正在執行管理任務而不是編程任務,這可能是多余的——學習像 PowerShell 這樣的工具有很大的長期優勢。
最后,如果您正在使用 web.config 的程序中修改 web.config,並且您沒有做任何太花哨或動態的事情,那么使用內置的Settings
或ConfigurationManager
可能是一種方式至 go。
您最好使用 MSBuild 腳本和MsBuild 社區任務XML 大規模更新任務來更改它
我個人建議使用 PowerShell。 這是 Microsoft 的下一代命令行,它位於 .Net 之上。 它被構建用於跨大量文件進行批量編輯等項目。
對於 SharePoint 環境中 web.config 的更改,您有專門為此任務開發的類。 您只需搜索SPWebConfigModification class 。
加載任意 .NET 配置文件
string configLocation = @"C:\myconfigFile.Config";
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileName = configLocation;
configFileMap.ExeConfigFilename = configFileName;
Configuration configuration= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
然后使用Razzie 的代碼來改變實際的配置設置
AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
if (appSettingsSection != null)
{
appSettingsSection.Settings["foo"].Value = "bar";
configuration.Save();
}
這就是我需要做的......謝謝所有的幫助!!!
// 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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.