Here is some code of my app.config file in my windows form application. I used this configuration file for the printer and scanner. I have used custom configurations in this xml. The problem I'm facing is whenever any attribute value is not set, My application throws a built in message error box like "The port com 4 does not exists"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Server" value="blah blah" />
<add key="Database" value="blah" />
<add key="User" value="blah" />
<add key="StoreID" value="blah" />
<add key="BranchID" value="blah" />
<add key="LotSize" value="100"/>
<add key="ZipsPrintingPath" value="blah.exe"/>
<add key="BCScannerPath" value="blah.exe"/>
<add key="SecuGenDeviceName" value="DEV_AUTO"/>
<add key="SecuGenPortAddress" value="USB_AUTO_DETECT"/>
<add key="SecuGenLiveTimeout" value="15000"/>
<add key="SecuGenImageQuality" value="50"/>
<add key="BCSPort" value="COM4"/>
<add key="BCSBaudRate" value="9600"/>
<add key="BCSParity" value="None"/>
<add key="BCSByteSize" value="8"/>
<add key="BCSStopBit" value="One"/>
<add key="BTicketBarcodeStartsWith" value="%B"/>
<add key="BTicketBarcodeEndsWith" value="."/>
<add key="TechSupportURL" value="blah"/>
</appSettings>
</configuration>
Here is also attached port error.
Now, I want to suppress this message box or I want to show this message in my custom message box.
Related
I want to add multiple int values to my Settings.
So far I have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>
Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.
I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString();
Note that your file wasn´t having the keys associated with any values. I added the value atribute.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
You could access the configuration section value using the ConfigurationManager GetSection method.
var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
var value = section["keyname"];
If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.
Created a C# Service Project in Visual Studio 2015, putting it together from various code sources.
It compiles, I can install the service, and I can start the service, but the service won’t fire the logic. It is supposed to look at a particular folder for only XML files, then upload them to an FTP site.
I added a logger and it writes out the following error consistently:
FTP Upload Service Started 05/08/2016 10:44:42 AM FTP Upload Service
Error on: 05/08/2016 10:44:42 AM Configuration system failed to
initialize at
System.Configuration.ClientConfigurationSystem.EnsureInit(String
configKey) at
System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String
sectionName) at
System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String
sectionName) at
System.Configuration.ConfigurationManager.get_AppSettings() at
FTPUploadService.Service1.ScheduleService() in
C:\user\Desktop\FTPUploadService\FTPUploadService\FTPUploadService\Service1.cs:line
51
I go to this particular line in the code (line 51), which is:
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
For reference here is more of the code block:
try
{
Scheduler = new Timer(new TimerCallback(SchedulerCallback));
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
this.WriteToFile("FTP Upload Service Mode: " + mode + " {0}");
//Set the Default Time.
DateTime scheduledTime = DateTime.MinValue;
if (mode == "DAILY")
{
//Get the Scheduled Time from AppSettings.
scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
}
if (mode.ToUpper() == "INTERVAL")
{
//Get the Interval in Minutes from AppSettings.
int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);
//Set the Scheduled Time by adding the Interval to Current Time.
scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next Interval.
scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
}
}
I’ve tried quite a few things, changing the XML format of the app.config, making sure the namespaces that are needed are there, checking the syntax of the XML for the app.config, to make sure it is correct…none have helped.
Here is the app.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Mode" value="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key="IntervalMinutes" value="1"/>
<add key="ScheduledTime" value="18:41"/>
<add key="sourceFiles" value="C:\XML Test\"/>
<add key="sourcePath" value="C:\XML Test\"/>
<add key="files" value="*.xml"/>
<add key="allDirectories" value="True"/>
<add key="addDateTimeBeforeUpload" value="True"/>
<add key="hostName" value="XXXXXXXXX"/>
<add key="Port" value="22"/>
<add key="UserName" value="XXXXXXX"/>
<add key="Password" value="XXXXXXXX"/>
<add key="SshHostKeyFingerprint" value=""/>
<add key="uploadpath" value="/XXXXXXX/"/>
<add key="removeSourceFiles" value="false"/>
<add key="SshPrivateKeyPath" value=""/>
<add key="protocol" value=""/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Any suggestions?? Issue with Configuration Manager?
I've run into this issue before and it is most likely because the xml in your app.config is not setup properly I believe the "configSections" node must be the first child node after "configuration". Check out my xml below and give that a try.
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<appSettings>
<add key="Mode" value="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key="IntervalMinutes" value="1"/>
<add key="ScheduledTime" value="18:41"/>
<add key="sourceFiles" value="C:\XML Test\"/>
<add key="sourcePath" value="C:\XML Test\"/>
<add key="files" value="*.xml"/>
<add key="allDirectories" value="True"/>
<add key="addDateTimeBeforeUpload" value="True"/>
<add key="hostName" value="XXXXXXXXX"/>
<add key="Port" value="22"/>
<add key="UserName" value="XXXXXXX"/>
<add key="Password" value="XXXXXXXX"/>
<add key="SshHostKeyFingerprint" value=""/>
<add key="uploadpath" value="/XXXXXXX/"/>
<add key="removeSourceFiles" value="false"/>
<add key="SshPrivateKeyPath" value=""/>
<add key="protocol" value=""/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
I have created a new mvc webapp with the following transforms for web.config:
Web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Debug" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Release" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Then I created a publish profile and called it Release, but I select the Debug(OBS! important) build configuration.
(I know. Stupid example. In my real project they were called Test and Test2.)
When I run the publish action I get the following in the transformed Web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
<add key="Release" value="true"/>
</appSettings>
Both the transformations were performed! Strange! If I change the name of the publish profile to Release2 I get following correct result:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
</appSettings>
What do you think? Bug?
Met same issue and finally was able to identify the reason and resolve it.
This issue happens because of publish profiles configuration settings mess. When you set up profile with configuration manager you should make sure Current Solution Configuration matches Configuration that is going to be used: Easiest way for configuration to track config Transformations
Otherwise, remember, that before selected Profile configuration, selected Configuration Transformation would be applied.
So, make sure you don't have twice assigned Configuration for different profiles. If so, just add one more configuration for 'failed' profile to resolve it and you get what you expected on transformation applied.
Why can't I read this from my web config? When I used App.config it worked fine..
FundGroups = null
private static readonly NameValueCollection FundGroups =
(NameValueCollection)ConfigurationManager.GetSection("FundGroups");
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="FundGroups" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<FundGroups>
<add key="Group1" value="Value1" />
<add key="Group2" value="Value2" />
<add key="Group3" value="Value3" />
<add key="Group4" value="Value4" />
<add key="Group5" value="Value5" />
<add key="Group6" value="Value6" />
<add key="Group7" value="Value7" />
</FundGroups>
</configuration>
I found a possible duplication here. But the ConfigurationSettings.GetConfig seems to be obsolete and doesnt work for me
I have asp.net mvc 4 project where try to use transform config, where have some default values and when I try to change to release mode some default values change to release values, but it isnt work. When I try to change to release mode and build application I have nothing changes. Here is my default values in Web.config:
<appSettings>
<add key="appId" value="####"/>
<add key="appSecret" value="####"/>
<add key="hostName" value="####"/>
</appSettings>
And here is what I have in my Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</appSettings>
</configuration>
Does anybody help me?
Using xdt:Locator="Match(name)", you're trying to match an attribute name which does not exist. You're looking for key for the appSettings:
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>