0

I have a very serious problem. A Customer has a SQL-Server. With Data. And the Application I made is only for Internal Usage. (Therefor there is no Webservice, etc. necessary) The Program works with a DataSet and its through an ConnectionString connected to the Database. The Connectionstring is stored in the App.Exe.Config which i didn't choose. It was made by the DataSet. The ConnectionString is at start empty. But the Data for the Connection is stored in an encrypted file. I am reading the Data and putting it into the connectionString.

The Code below works perfectly when I'm debugging.

BUT the Problem is now: I've created an Installer and now after the installation in the Program Files Folder it says it has no authorization to save the config. I mean I know what that means, and all. But is there a way that the application can save it? When i Opened it as an Administrator it saved the config. Set Full Access to "Everyone" didn't work.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection =(ConnectionStringsSection)config.GetSection("connectionStrings"); connectionStringsSection.ConnectionStrings["ApplicationName.Properties.Settings.managementSQLConnectionString"].ConnectionString = String.Format(@"Data Source={0}\{1};Initial Catalog={2};User ID={3};Password={4};MultipleActiveResultSets=True", _DatabaseLocation, _DatabaseName, _Catalog, _Username, _Password);
try {
    config.Save(ConfigurationSaveMode.Full);
}
catch (Exception err) {
    NotificationController.Notify("Config-Error", err.Message, 
    System.Windows.Forms.ToolTipIcon.Warning);
}

ConfigurationManager.RefreshSection("connectionStrings");
  • Possible duplicate of [How do I force my .NET application to run as administrator?](https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator) – Broots Waymb Oct 23 '18 at 15:32
  • Instead of saving it in the app.exe.config file, you should be storing it in a custom configuration file and keep that file in the AppData directory. Don't force the user to run your program as Administrator or modify the security ACL for the Program Files directory (which is dangerous). App.exe.config is for configuration values that never change, especially if that application is installed to Program Files. – Ron Beyer Oct 23 '18 at 15:50
  • Or, alternatively you can store the `app.config.exe` in the AppData directory and use [`ConfigurationManager.OpenMappedExeConfiguration`](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openmappedexeconfiguration?view=netframework-4.7.2) instead to point it to the AppData directory. – Ron Beyer Oct 23 '18 at 15:52
  • Never force user to run application in Admin, espcially in an entreprise environment. Create a Class to hold your settings and use Serialization to save/load them from a AppData folder: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData). You can see nearly all applications are writing their settings there. – h.yuan Oct 23 '18 at 16:55
  • @RonBeyer currently i am saving a config file in the appdata folder for the data of the user. But the DataSet require the connectionString in the exe.config. is there a way the DataSet saves the exe.config in the appdata? because when i created the dataset. the connectionString was automatically saved in the exe.config which I did not find out how to change this. – Jagl Keilhauer Oct 25 '18 at 10:28
  • @RonBeyer Nevermind your Alternative Solution with the OpenMappedExeConfiguration worked for me perfectly. Thanks – Jagl Keilhauer Oct 25 '18 at 13:52

0 Answers0