views:

489

answers:

1

I made a simple WPF application in Visual Studio.

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

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

I clicked Publish and basically accepted all the defaults (CD/DVD publishing, no online updates).

I copied the setup folder to another computer.

On the new computer I double-clicked setup.exe.

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

alt text

I've given the application all the permissions I can in project/properties/security:

alt text

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

ADDENDUM The fix in the answer below works for Windows 7, but on Windows XP, I get this:

alt text

+2  A: 

First, you are deploying it as data, and your connection string shows this. DataDirectory translates to the Data folder under the ClickOnce cache, and this is where ClickOnce puts the file if you have it marked as data.

If you don't want it deployed here, but want it relative to the [exe], go into Application Files dialog in the Publish tab and change it from Include(Data) to just Include. Then it will put it in a location relative to the location of the exe. (I.E. if you have it in a folder in your project, it will put it in the corresponding folder under the deployed exe). Don't forget to change your connection string.

If you deploy it in that location, it will be lost when you issue updates. (Just so you know). Here is an idea about where else you can put your data to avoid this problem:

How to keep your data safe from ClickOnce updates

You need to create the connection string dynamically, Path.Combine(System.Windows.Forms.Application.StartupPath, "Data\mydatabase.mdf"); or it will start in the default path of the current assembly (.NET in your case).

--addition--

Argh, WPF, sorry, missed that. You basically need to get the folder your exe is running in. See if this helps:

In the System.Reflection namespace:

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

(In VSTO apps, you have to use CodeBase because of the shadowing of the dll. I think Location will work for regular app.)

RobinDotNet

RobinDotNet