We have a SharePoint server which I can reference through the following:
//DCP connection
private static dcp.Lists lists = new dcp.Lists();
lists.Credentials = System.Net.CredentialCache.DefaultCredentials;
lists.Url = Values.SERVERADDRESS + "/_vti_bin/lists.asmx";
This works through a port which only has Domain authentication (SERVERADDRESS value http://dcp:17649).
However, our browser uses other authentication methods through http:// and https://.
These are Claims-based and contain Forms-based authentication for external users and NTLM authentication for users on the domain.
However, I seem unable to access this through these ports.
Here's my code:
//create networkCredential
System.Net.NetworkCredential nc = System.Net.CredentialCache.DefaultNetworkCredentials;
System.Net.CredentialCache cc = new System.Net.CredentialCache();
//add into CredentialCache and speicfy the NTLM authType
cc.Add(new Uri(Values.SERVERADDRESS + "/_vti_bin/lists.asmx"), "NTLM", nc);
lists.Credentials = cc;
lists.Url = Values.SERVERADDRESS + "/_vti_bin/lists.asmx";
the SERVERADDRESS value is http://dcp here.
There are several posts all over the internet (also here) which have something to do with changing the app.config. However, mine looks totally different than the ones you can find in these posts:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="DriverDevPlanning.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<applicationSettings>
<DriverDevPlanning.Properties.Settings>
<setting name="DriverDevPlanning_dcp_Lists" serializeAs="String">
<value>http://dcp/_vti_bin/Lists.asmx</value>
</setting>
<setting name="DriverDevPlanning_dcp_UserGroup_UserGroup" serializeAs="String">
<value>http://dcp/_vti_bin/usergroup.asmx</value>
</setting>
</DriverDevPlanning.Properties.Settings>
</applicationSettings>
</configuration>
Related
My app is working fine when debugging but when I'm trying to run it outside visual studio it just keeps crashing and all what I can find is this in event viewer:
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(System.Object, System.Configuration.Internal.InternalConfigEventArgs)
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.RefreshSection(System.String)
at System.Configuration.ClientSettingsStore.ReadSettings(System.String, Boolean)
at System.Configuration.LocalFileSettingsProvider.GetPropertyValues(System.Configuration.SettingsContext, System.Configuration.SettingsPropertyCollection)
at System.Configuration.SettingsBase.GetPropertiesFromProvider(System.Configuration.SettingsProvider)
at System.Configuration.SettingsBase.GetPropertyValueByName(System.String)
at System.Configuration.SettingsBase.get_Item(System.String)
at System.Configuration.ApplicationSettingsBase.GetPropertyValue(System.String)
at System.Configuration.ApplicationSettingsBase.get_Item(System.String)
at LeagueFPSBoost.Properties.Settings.get_UpgradeRequired()
at LeagueFPSBoost.Program.CreateConfigIfNotExists()
at LeagueFPSBoost.Program.Startup(System.String[])
at LeagueFPSBoost.Program.Main(System.String[])
It was working fine, i just dont know what happened to it. Here is more info:
static void CreateConfigIfNotExists()
{
var configFile = $"{Application.ExecutablePath}.config";
if (!File.Exists(configFile))
{
File.WriteAllText(configFile, Resources.App_Config);
}
var configDir = Path.Combine(leagueConfigDirPath, #"LeagueFPSBoost\");
if (!Directory.Exists(configDir))
{
Directory.CreateDirectory(configDir);
}
if (!File.Exists(Path.Combine(configDir, "App.config")))
{
File.WriteAllText(Path.Combine(configDir, "App.config"), Resources.App_Config);
}
AppConfig.Change(Path.Combine(configDir, "App.config"));
if (File.Exists(configFile))
{
File.Delete(configFile);
}
if (Settings.Default.UpgradeRequired) //Exception Thrown Here
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
}
}
And this is from Settings.Desinger.cs:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool UpgradeRequired {
get {
return ((bool)(this["UpgradeRequired"]));
}
set {
this["UpgradeRequired"] = value;
}
}
Here is App.config:
<?xml version="1.0" encoding="utf-8"?>
<!-- User settings like theme and etc are stored in ...\AppData\Local\LeagueFPSBoost\... -->
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="LeagueFPSBoost.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<userSettings>
<LeagueFPSBoost.Properties.Settings>
<setting name="ThemeStyle" serializeAs="String">
<value>Light</value>
</setting>
<setting name="ColorStyle" serializeAs="String">
<value>Blue</value>
</setting>
<setting name="Notifications" serializeAs="String">
<value>True</value>
</setting>
<setting name="Log" serializeAs="String">
<value>True</value>
</setting>
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
</LeagueFPSBoost.Properties.Settings>
</userSettings>
</configuration>
When application starts from Visual Studio in debug mode everything works fine every time. Problem is when I run application by clicking on exe it works first time (if user.config doesn't exist in roaming/local/appname.. folder). One I run application for second time by clicking on exe I just get the same error every time. But when I delete user.config file it starts again normally but it just wont start with user.config being there....
This is user.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<LeagueFPSBoost.Properties.Settings>
<setting name="ThemeStyle" serializeAs="String">
<value>Dark</value>
</setting>
<setting name="ColorStyle" serializeAs="String">
<value>Red</value>
</setting>
<setting name="Notifications" serializeAs="String">
<value>True</value>
</setting>
<setting name="Log" serializeAs="String">
<value>True</value>
</setting>
<setting name="UpgradeRequired" serializeAs="String">
<value>False</value>
</setting>
</LeagueFPSBoost.Properties.Settings>
</userSettings>
</configuration>
Sometimes I'm also getting this exception:
System.Configuration.ConfigurationErrorsException: Unrecognized configuration section userSettings
(C:\Users\...\AppData\Local\LeagueFPSBoost\LeagueFPSBoost.exe_Url_10kxs4....xzo1\2.8.0.0\user.config line 3)
I tried incrementing 2.8.0.0 to 2.9.0.0 but I still got same problem.
It was working fine before, I don't know what happened to it.
Edit: I found out where exception is thrown by attacking debugger to process but still I don't know why?
Edit 2: I found out that adding:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="LeagueFPSBoost.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
to the user.config fixes the problem but why is it missing in there in first place?
I've found answer here.
I have modified a code a bit so that its universal:
/// <summary>
/// Corrects the roaming settings file if needed because sometimes the node "configSections" is missing in the settings file.
/// Correct this by taking this node out of the default config file.
/// </summary>
private static void CorrectRoamingSettingsFileIfNeeded()
{
const string NODE_NAME_CONFIGURATION = "configuration";
const string NODE_NAME_CONFIGSECTIONS = "configSections";
const string NODE_NAME_USERSETTINGS = "userSettings";
//Exit if no romaing config (file) to correct...
var configRoaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
if (configRoaming == null) return;
if (!configRoaming.HasFile) return;
//Check for the <sectionGroup> with name="userSettings"
//Note: Used ugly iteration because "configRoaming.GetSectionGroup(sectionGroupName)" throws ArgumentException.
ConfigurationSectionGroup sectionGroupUserSettings = null;
foreach (ConfigurationSectionGroup sectionGroup in configRoaming.SectionGroups)
{
if (sectionGroup.Name.Equals(NODE_NAME_USERSETTINGS))
{
sectionGroupUserSettings = sectionGroup;
break;
}
}
//Exit if the needed section group is found...
if (sectionGroupUserSettings != null && sectionGroupUserSettings.IsDeclared) return;
//Do correction actions...
var xDoc = XDocument.Load(configRoaming.FilePath);
var userSettingsNode = xDoc.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_USERSETTINGS);
var ConfigFullFilename = Assembly.GetEntryAssembly().Location;
var configDefault = ConfigurationManager.OpenExeConfiguration(ConfigFullFilename);
var xDocDefault = XDocument.Load(configDefault.FilePath);
var configSectionsNode = xDocDefault.Element(NODE_NAME_CONFIGURATION).Element(NODE_NAME_CONFIGSECTIONS);
userSettingsNode.AddBeforeSelf(configSectionsNode);
xDoc.Save(configRoaming.FilePath);
}
That was confusing as hell....
While this is not directly related to the OP, this is the post users are sent to when searching for this error, so it is worth pointing out here as it is related.
This error occurs in .Net Core when adding an app.config using system.Configuration.ConfigurationManager 4.7.0:
ConfigurationErrorsException: Unrecognized configuration section add. (C:\Source\Repos\SFTP_Application\SFTP_Application\SFTP_Application_Console\bin\Debug\netcoreapp3.1\SFTP_Application_Console.dll.config line 3)
Unrecognized configuration section add means: it is missing a "section" and it thinks that <add> is a section.
In .Net Core's case, it is originally built to run off .json, but not everyone likes running their site off .json alone. So, you can add a .config file, but out of the box that file is missing an <appSettings> section. Put your keys in that, and your good to go.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ApplicationId" value="0" />
<!--SqlConnections-->
<add key="sql_Con_String" value="mydsstring" />
</appSettings>
</configuration>
What is this Error.
System.ArgumentException: 'Keyword not supported: 'metadata'.'
Line of follow is my app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Foroush.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<connectionStrings>
<add name="ForoushEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Foroush;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<applicationSettings>
<Foroush.Properties.Settings>
<setting name="Foroush_SmsWebService_SendReceive" serializeAs="String">
<value>http://ip.sms.ir/ws/SendReceive.asmx</value>
</setting>
</Foroush.Properties.Settings>
</applicationSettings>
</configuration>
For Example
In the screenshot I see you alter the connection string by replacing an important part with "" in an attempt to create a Sql Connection String instead of an Entity Framework Connection String.
There are build-in methods for that like:
var entityConnectionStringBuilder = new EntityConnectionStringBuilder(MyConnectionString);
string sqlConnectionString = entityConnectionStringBuilder.ProviderConnectionString;
return new System.Data.SqlClient.SqlConnectionStringBuilder(sqlConnectionString).UserId);
In the screenshot code you probably wanted to use ConnectionStringNewFormat as an argument in the SqlConnectionStringBuilder instead of MyConnectionString (which is the original Entity Framework Connection String. But please, do not use string manipulation but use the code provided to you by the Entity Framework as demonstrated above.
I am trying to update value of a particular setting in app.config.exe at run time. But as per my code its getting updating in vshost32.exe file which seem to duplicate as app.config.exe. My code is given below
private void btnSubmit_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
string d = applicationSectionGroup.Sections[0].ToString(); ;
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["Secure_Browser_CS_Version.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
//Encryption Key Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("NavigateURL");
applicationSetting.Value.ValueXml.InnerXml = txtURL.Text;
applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();
}
// app.config.exe
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyApplicationName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<MyApplicationName.Properties.Settings>
<setting name="NavigateURL" serializeAs="String">
<value></value>
</setting>
</MyApplicationName.Properties.Settings>
</applicationSettings>
</configuration>
It seems you are doing everything correct, but since you are running using vshost.exe, this is the exe whose configuration is updated.
To run without vshost, uncheck the "Enable the Visual Studio hosting process" checkbox in Project prroperties -> Debug tab:
what I have in app.config is this
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Porject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<applicationSettings>
<Porject.Properties.Settings>
<setting name="PhotoLocation" serializeAs="String">
<value>.\photos\</value>
</setting>
</Porject.Properties.Settings>
</applicationSettings>
<connectionStrings>
<add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>
</configuration>
and this is how I call the PhotoLocation:
string s = ConfigurationManager.AppSettings["PhotoLocation"];
When I make a request to get the connectionstring, there is no problem and it works fine; but when I requst the PhotoLocation it returns null.
Also the ConfigurationManager.AppSettings.Count returns 0.
Does anyone have an idea on what I'm doing wrong?
simply add this in your App config file
<appSettings>
<add key="PhotoLocation" value=".\photos\"/>
<appSettings>
ConfigurationManager.AppSettings reads (as the name might suggest) the AppSettings block of your configuration.
Given you've created your own section, you want to use var section = ConfigurationManager.GetSection("Porject.Properties.Settings") and read the values from that section.
I have saved strings in a dll application's setting. I want to retireve them.
Here is the configuration file for my dll:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxxxxxxx" >
<section name="Search.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<PishiSearch.Properties.Settings>
<setting name="ReadIndex" serializeAs="String">
<value>C:\Index</value>
</setting>
<setting name="WriteIndex" serializeAs="String">
<value>C:\WriteIndex</value>
</setting>
</PishiSearch.Properties.Settings>
</applicationSettings>
</configuration>
It resides in the same directory as my dll. It is called: Search.dll.config
My dll is called: Search.dll
I want to read the values of ReadIndex and WriteIndex from this config file into my dll.
Here is the code:
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //count of this is 21
ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
ConfigurationSectionCollection csc = csg.Sections;
ConfigurationSection cs = csc.Get("Search.Properties.Settings");
The code works up to getting the last line here. But how do I get the settings strings?
Yes I can use cs.SectionInformation.GetRawXml(); to get the xml and then interrogate it to get the values, but that is a kluge.
How do I read the values? Preferably into a Settings object? Many thanks!
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<applicationSettings>
</applicationSettings>
<appSettings>
<add key="ReadIndex" value="C:\Index"/>
</appSettings>
</configuration>
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //count of this is 21
string s = config.AppSettings.Settings["ReadIndex"].Value.ToString();
you must add tag "appSettings" into tag "configuration" in your file "app.config" in visual studio
like the bellow:
<configuration>
<appSettings>
<add key="ReadIndex" value="aaa"/>
<add key="WriteIndex" value="111"/>
</appSettings>
</configuration>
and then use this bellow code in c#
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string userName = appConfig.AppSettings.Settings["ReadIndex"].Value;
string password = appConfig.AppSettings.Settings["WriteIndex"].Value;
if you want to update your configuration you can open the "Search.dll.config" file and then update it.
please refer to the bellow answer:
Reading dll.config (not app.config!) from a plugin module