How can I deploy a WPF application using ClickOnce, which accesses a local MDF database file?

I made a simple WPF application in Visual Studio.

It accesses its database file in your own directory using this connection string:

System.Environment.CurrentDirectory + @"\Data\" + databaseFileName;

I clicked Publish and basically accepted all the default settings (publishing to CD / DVD, without online updates).

I copied the installation folder to another computer.

On the new computer, I double-click setup.exe .

It installs fine, is in the start menu, but when it starts, it gets this error :

alt text http://www.deviantsart.com/upload/11jfc2c.png

I gave the application all the permissions that I can in the project / properties / security:

alt text http://www.deviantsart.com/upload/si6urj.png

What I need to change so that this application deployed with ClickOnce can find its database in the relative directory under the .exe file, i.e. "Data / MainData.mdf"?

ADDITION The fix in the answer below works for Windows 7, but in Windows XP I get the following:

alt text http://www.deviantsart.com/upload/1pds19l.png

+3
source share
1 answer

, . DataDirectory "" ClickOnce, ClickOnce , .

, , , [exe], " " "" Include (Data) "". exe. (I.., , exe). .

, . ( ). , , :

ClickOnce

, Path.Combine(System.Windows.Forms.Application.StartupPath, "Data\mydatabase.mdf" ); (.NET ).

- -

, WPF, , . , exe. , :

System.Reflection:

Assembly assemblyInfo = Assembly.GetExecutingAssembly();
if (assemblyInfo != null)
{
    //try Path.GetDirectoryName(assemblyInfo.Location)
    //if that doesn't work, try assemblyInfo.CodeBase 
}

( VSTO CodeBase - dll. , Location .)

RobinDotNet

+4

Source: https://habr.com/ru/post/1728694/


All Articles