简体   繁体   中英

C# WinForm application - How to save connection string (SQLite)

I have a winform app in .NET 4.0 with database (SQLite). I need to add an option for user to be able to change the path to the file containing the database - in short, to change the connection string. I used app.config to save connection string like this:

<connectionStrings>
<add name="connectionString" connectionString="Data Source=D:\myDatabase.db; FailIfMissing=True; Version=3"/>
</connectionStrings>

Is there a way to allow user to modify app.config file (it is located in Program Files folder)? I do not want to run the app as an administrator, is there a way to give administrator rights to user temporarely, only when trying to modify app.config file?

If you have any better suggestion for persistently storing connection string, please share.

Thank you!

EDIT: I can actually change app.config file:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = "Data Source=" + path + "; FailIfMissing=True; Version=3";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

Problem is when user is not an administrator, access to the file is denied. Is there a workaround (I do not want to run the app as administrator)? Or is there a better way to store my connection string?

You should make use of special folders. Either LocalApplicationData (applies to current user - local only) or ApplicationData (applies to current user - roaming):

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

This is where you should store path \\ connection string to the db file selected by user. These folders are created for all user profiles on Windows OS. For example, if your logon name is "Hari" then the path would be "C:\\Users\\Hari\\AppData\\Local". Hari has full access to this path so he can write to it.

Also, this allows multiple users to use your software. They won't step on each other. All of them will have their own DB files. If this is not what you desire, then you may use other special folders (like CommonApplicationData) mentioned here:

https://msdn.microsoft.com/en-us/library/system.environment.specialfolder%28v=vs.110%29.aspx

However to use some of them you may have to have admin rights. (For the two special folders I described above, you don't need admin rights.) In that case you could ask for DB file path at the installation time.

I think what you want to do is use Settings (instead of using the app.config). See this: https://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx

It's basically the same as the config file, but gives the user (and you) easy access.

First set a default connection string in the designer: 在此处输入图片说明

Then access it like textBox1.Text = Properties.Settings.Default.connectionString;

Do the reverse to save it: Properties.Settings.Default.connectionString = textBox1.Text;

And make sure you save: Properties.Settings.Default.Save();

This could help:

private static void UpdateSetting(string key, string value)
{
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    configuration.AppSettings.Settings[key].Value = value;
    configuration.Save();

    ConfigurationManager.RefreshSection("appSettings");
}

From: App.Config change value

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