Currently, in my web.config file I have
<configuration>
<general path="c:\abc\" />
</configuration>
I want to change c: to d: when I publish the release version.
How do I do this in a transform?
The <general> section is much much bigger so I don't want to rewrite then entire thing, just that one attribute. Can anyone help?
Update: I created the following web.release.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<General dataFilePath="D:\Data" xdt:Transform="SetAttributes" xdt:Locator="Match(dataFilePath)" />
<AuditManagement auditPath="D:\Audit" xdt:Transform="SetAttributes" xdt:Locator="Match(auditPath)" />
</configuration>
This had no effect on the final web.config. It still shows "C:\" where I would want "D:\"
You can try adding these attributes to the Web.Release.config:
xdt:Transform="SetAttributes" xdt:Locator="Match(path)"
so your final result should be:
<configuration>
<general path="d:\abc\" xdt:Transform="SetAttributes" xdt:Locator="Match(path)"/>
</configuration>
I would recommend you to use transformation
First you have to create the different enviroments for debug or release, and if you want you can add more.
This tutorial is well explained and tested by me:
http://deanhume.com/home/blogpost/working-with-multiple-web-config-files/4100
And here is the official documentation:
https://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
as it is explained in the previous your configuration for the web.config will be:
<configuration>
<general path="d:\abc\" xdt:Transform="SetAttributes" xdt:Locator="Match(path)"/>
</configuration>
and then you will have to define the correct path in the Debug and Realease configs, once it's finished you will run the app with one of the configurations you set.
Related
Note:
I've searched for this on Google and didn't directly find anything related to WinForms. The suggested questions while typing this question didn't apply to my situation.
I have a WinForms (.NET 3.5) application that can be published/deployed to three different locations: development, test and production. Each deployment type has its own specific settings, for example: connectionString, title(s), webservice address, directories/files to read or other settings.
The current situation in the app.config is as following:
<!-- dev-->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="dev" />
<add key="SourceDir" value="\\server\foo\dev" />
</appSettings>
...
</configuration>
<!-- test -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="test" />
<add key="SourceDir" value="\\server\foo\test" />
</appSettings>
...
</configuration>
<!-- prod -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="prod" />
<add key="SourceDir" value="\\server\foo\prod" />
</appSettings>
...
</configuration>
And everytime the app needs to be published/deployed to a certain location, the other two are commented out. So if I build a DEV-release, I comment out the PROD and TEST sections. When publishing for production, ... you get the idea.
This is a useless time-consuming work. My question is: is there a way to avoid this? A way that I still have the separate deployment-specific configuration, but that I don't need to comment (out) the needed sections.
I hope you can help me.
I'm supposed to add a new type of values to an AppSettings file (already existing with some values). Those values are a whole list of special folders so I thought the best way would be to have a new section for those folder values so that the file would look like this:for
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="usPath" value="folderName1" />
<add key="tcPath" value="folderName2" />
<add key="usGUID" value="folderID1" />
<add key="tcGUID" value="folderID2" />
</appSettings>
<updateFolders>
<add key="folderID3" value="folderName3">
<add key="folderID4" value="folderName4">
</updateFolders>
</configuration>
Reading and writing within the already existing appSettings-tag is no problem but I haven't find a way yet to modify the updateFolders section. I'm really new to using AppSettings in this way so I don't know too much about what's possible and what's not. In addition to that I think the AppSettings file might have been set up in a wrong way from the very beginning (it gets created by using a System.IO.File-Writer).
see ConfigurationManager.GetSection
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection(v=vs.110).aspx
I have to add some constants to MVC 4 project.
Adding them to in web.config will work:
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
Is there a way to create separate config file for this constants?
I guess you want to separate your appSettings from web.config, you can store them in a separate file and then specify that in your web.config under appSettings's configSource like:
<appSettings configSource="MySettings.config" />
and then you can have your settings in MySettings.config as:
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
You may see: Using configSource to split configuration files
If you put your code snippet in a file called appSettings.config, you can simply reference that file in your web config:
<appSettings configSource="appSettings.config" />
I am trying to add a set of URL Rewriting rules but only to the published version of the web site I'm developing.
I find all sorts of examples if i want to change say the connection string value, but I cannot find an example of how to add something that does ot already exists in the main web.config.
What I need is to add the rewrite node under the system.WebServer.
All you need is to use xdt:Transform="Insert" attribute within a tag that you want added during transformation. Read more about it here: http://msdn.microsoft.com/en-us/library/dd465326.aspx
You can take the below sample as a starting point (which is my Web.Release.config file):
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- Enable static content caching in release mode -->
<system.webServer xdt:Transform="Insert">
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="5.00:00:00" cacheControlCustom="public" />
</staticContent>
</system.webServer>
</configuration>
This is my first time using an XML config file. In the solution explorer I right click, add, new item, application config. file.
I then edited the file to read...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key1" value="1000" />
</appSettings>
</configuration>
...and tried to access it with...
Int32.Parse(ConfigurationManager.AppSettings.Get("Key1"));
...but it says that the parameter is null. I looked at just ConfigurationManager.AppSettings and it's AllKeys property has dimension 0.
What am I doing wrong?
Thanks!
Right-click on your project, choose Properties>Settings tab and edit your settings there because the config file is not the only place these values are stored at.
In your code use Settings.Default.MySetting to access the settings.
You'll need to add using MyProjectName.Properties; to your using statements in order to access the settings this way or you'll have to fully qualify it as MyProjectName.Properties.Settings.Default.MySetting...
I know this is super old but I just encountered this opening an old project. A reference to System.Configuration was missing.
To ensure get information from App.config using ConfigurationManager.AppSettings["<Key name>"]; be sure to set your values inside <appSettings> section.
Example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key ="IntervalSeconds" value ="60000"/>
<add key ="OutputPathLog" value ="\\myserver\hackingtrack\Projects\Log\"/>
<!--Jorgesys si Elenasys sunt puisori-->
<add key="InputFeeds1" value="https://cld.blahbla.com//portadaKick.json" />
<add key="InputFeeds2" value="https://cld.blahbla.com//portadaKick.json" />
<add key="InputFeeds3" value="https://cld.blahbla.com//portadaKick.json" />
</appSettings>
</configuration>
Retrieving values:
string intervalSeconds = ConfigurationManager.AppSettings["IntervalSeconds"];
string inputFeeds1 = ConfigurationManager.AppSettings["InputFeeds1"]; ;
string inputFeeds2 = ConfigurationManager.AppSettings["InputFeeds2"];
string inputFeeds3 = ConfigurationManager.AppSettings["InputFeeds3"];