System.Configuration.ConfigurationManager.AppSettings - question

T

Thomas Bauer

Hello,

I have a config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Directory" value="C:\_Database\" />
<add key="CloseApplication" value="0" />
<add key="Extension" value="*.txt,*.log" />
</appSettings>
</configuration>


I read it.


string Directory;
bool CloseApplication;


Directory =
System.Configuration.ConfigurationManager.AppSettings["Directory"];
string help =
System.Configuration.ConfigurationManager.AppSettings["CloseApplication"];
Int32 ret = Convert.ToInt32( help );
// #####// CloseApplication = Convert.ToBoolean( help );


if ( ret == 0 )
CloseApplication = false;
else
CloseApplication = true;


A) Why can I not convert to bool? // #####// CloseApplication =
Convert.ToBoolean( help );
B) All values are string in ConfigurationManager?
C) It is possible in the direct way boolean, double, etc?


Regards Thomas
 
M

Mr. Arnold

Thomas Bauer said:
Hello,

I have a config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Directory" value="C:\_Database\" />
<add key="CloseApplication" value="0" />
<add key="Extension" value="*.txt,*.log" />
</appSettings>
</configuration>


I read it.


string Directory;
bool CloseApplication;


Directory =
System.Configuration.ConfigurationManager.AppSettings["Directory"];
string help =
System.Configuration.ConfigurationManager.AppSettings["CloseApplication"];
Int32 ret = Convert.ToInt32( help );
// #####// CloseApplication = Convert.ToBoolean( help );


if ( ret == 0 )
CloseApplication = false;
else
CloseApplication = true;


A) Why can I not convert to bool? // #####// CloseApplication =
Convert.ToBoolean( help );
B) All values are string in ConfigurationManager?
C) It is possible in the direct way boolean, double, etc?

(Another Way to read/write configuration values)

http://geekswithblogs.net/akraus1/articles/64871.aspx

You can make it a Bool right out of the gate by using the example, and you
can call the examples by name anything you want, like ConfigData --
ConfigSettings.

And with in the Configdata.cs

[ConfigurationProperty("closeapplication")]
public bool CloseApplication {

get { return (bool)this["closeapplication"]; }

set{ this["closeapplication"] = value; }

}
 
T

Thomas Bauer

Hello,
You can make it a Bool right out of the gate by using the example, and you
can call the examples by name anything you want, like ConfigData --  
ConfigSettings.

And with in the Configdata.cs

         [ConfigurationProperty("closeapplication")]
            public bool CloseApplication {

                  get { return (bool)this["closeapplication"]; }

                  set{ this["closeapplication"] = value; }

            }
OK how?
With a assistent?

Regards Thomas

Error 1 The type "ConfigurationProperty" is not found. (Missing using
directive or assembly relation?)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace RFileCleaner
{
public partial class frmRFileCleaner : Form
{
string Directory;
bool CloseApplication;
Int32 DeleteFilesOlderThanDays;

[ConfigurationProperty( "CloseApplication" )]
public bool CloseApplication2
{
get { return (bool)this["CloseApplication"]; }
set { this["CloseApplication"] = value; }
}

public frmRFileCleaner( )
{
InitializeComponent();

//<add key="Directory" value="C:\_Database\" />
//<add key="CloseApplication" value="0" />

Directory =
System.Configuration.ConfigurationManager.AppSettings["Directory"];
string help =
System.Configuration.ConfigurationManager.AppSettings["CloseApplication"];
Int32 ret = Convert.ToInt32( help );

if ( ret == 0 )
CloseApplication = false;
else
CloseApplication = true;

help =
System.Configuration.ConfigurationManager.AppSettings["DeleteFilesOlderThanDays"];
DeleteFilesOlderThanDays = Convert.ToInt32( help );

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top