简体   繁体   中英

C# app.config read and write

Can we save data or some text in app.config file if yes then it is persistence or temporary ?

for example if i store last access date time in app.config file and then close/Exit the application after the some time/days/years when i start my application is it possible that the last access date time I can retrieve. If yes then how please explain with code ....

Thanks, Raj

here is my code ..

Trying to retrieve date time from config file.. But show error object not set to be an object like...

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if (email.To.Contains(Email) && DateTime.Compare(email.UtcDateTime.Date, Convert.ToDateTime(config.AppSettings.Settings["ModificationDate"].Value)) > 0)

Here i store /save the date time in app.config file.

System.Configuration.Configuration config =
             ConfigurationManager.OpenExeConfiguration
                        (ConfigurationUserLevel.None);

// Add an Application Setting.
config.AppSettings.Settings.Add("ModificationDate", DateTime.Now + " ");

// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Console.WriteLine("Last Update :"+config.AppSettings.Settings["ModificationDate"].Value);
Console.ReadLine();

Please suggest me why it show me an error that object not set am done any mistake please ans...

You can create a settings xml file to do this.

In VS go to your project properties -> Settings , then write a value in.

In code, you can get/set that value using

Properties.Settings.Default.YourVariable = 0;

If you are setting the value make sure to save it

Properties.Settings.Default.Save();

See here for a good tutorial.

I would use a custom configuration section to create your configuration type - last access date time. you can create more complex hierarchy configuration and strongly-typed.

Again, it depend on your requirement and design if you need to do that. otherwise, standard file storage(txt/xml) would also allow you to do that. I personally normally using app.config for application specific level(appearance/fonts/servername etc.) configuration rather than transactional configuration.

for eg

public class LastAccessConfigurationSection : System.Configuration.ConfigurationSection {
    [ConfigurationProperty("LastAccess")]
    public string LastAccess{
        get { return (string)this["LaunchTime"]; }
        set { this["LaunchTime"] = value; }
    }
}

you can have a static class to manage the life-cycle that would allow you to persist the change.

public static LastAccessConfigurationSection Config { get; internal set; }

    public static void Initialize() {
        Config = ConfigurationManager.GetSection("LastAccess") as LastAccessConfigurationSection;
    }

You could use App.config for storage like any other file and it will persist and be available the next time you run your program. That is the nature of file storage in general. I would suggest that you store that data in a separate file or database. I will not, however, write the code for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM