VS2008 Setup Project: Shared (By All Users) Application Data Files?

后端 未结 7 679
深忆病人
深忆病人 2020-11-27 13:41

fellow anthropoids and lily pads and paddlewheels!

I\'m developing a Windows desktop app in C#/.NET/WPF, using VS 2008. The app is required to install and run on Vis

7条回答
  •  野性不改
    2020-11-27 14:23

    I had the same issue. The setup project gives the user the option to install the app "for the current user only" or "for all users:. Consequently, the database file would end up in either the current user's or the All Users application data folder. The setup would have to write this information somewhere so that the application can later retrieve it, when it comes to accessing the database. How else would it know which application data folder to look in?

    To avoid this issue, I just want to install the database in the All Users/Application Data folder, regardless if the application was installed for one user or for all users. I realize, of course, that two users could not install the application on the same computer without overwriting each other's data. This is such a remote possibility, though, that I don't want to consider it.

    The first piece of the puzzle I got here:

    Form_Load(object sender, EventArgs e)
    {
      // Set the db directory to the common app data folder
      AppDomain.CurrentDomain.SetData("DataDirectory", 
                System.Environment.GetFolderPath
               (System.Environment.SpecialFolder.CommonApplicationData));
    }
    

    Now we need to make sure that the data source contains the DataDirectory placeholder. This piece came from here. In the DataSet designer, find the DataSet's properties, open the Connection node and edit the ConnectionString property to look as follows:

    Data Source=|DataDirectory|\YourDatabase.sdf
    

    Then I followed Lyman Enders Knowles' instructions from above for how to add the Common Application Data Folder to the setup project and placed the database file in that folder.

    I then followed Ove's suggestion from above, i.e. I checked the "Enable ClickOnce Security Settings" and selected "This is a full trust application.

    After that, the application deployed fine on Vista and the database file was accessible for both reads and writes.

提交回复
热议问题