开发者

How to get the directory of database?

开发者 https://www.devze.com 2023-03-04 04:04 出处:网络
I am using the Microsoft database that is inside C:\\wpf开发者_高级运维1\\WpfApplication1\\WpfApplication1 folder. When I update the database it always updates the one inside C:\\wpf1\\WpfApplication1

I am using the Microsoft database that is inside C:\wpf开发者_高级运维1\WpfApplication1\WpfApplication1 folder. When I update the database it always updates the one inside C:\wpf1\WpfApplication1\WpfApplication1\bin\Debug which I don't want.

How do I get the folder C:\wpf1\WpfApplication1\WpfApplication1 without typing this full name?


There is an article here about sql server compact edition

I dont think anything has changed in the newer releases although it is a while since i have used it.

|DataDirectory| works as follows

Your connection string would normally look like this “Data Source = |DataDirectory|\Mydb.sdf”

To set the DataDirectory property, call the AppDomain.SetData method.

If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:

For applications that are put in a folder on the user's computer, the database folder uses the application folder. (this is the default rule that applies when debugging and hence why the database in \bin\debug is the one that is updated)

For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.

In the past i have done the following

Handle the SettingsLoaded event in settings.vb

        Private Sub MySettings_SettingsLoaded(sender As Object, e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

        If Not Debugger.IsAttached Then
            My.Settings.SQLCEConnectionString = "somefolder\somefile.sdf"
        End If

    End Sub


The directory C:\wpf1\WpfApplication1\WpfApplication1 is your project directory - its the one that the source code is in, but its not normally a directory that your application would usually be aware of or work with (for example if your application is installed to another PC then this directory simply won't exist).

The usual approach when working with databases or dependent files is to either:

  • Put the file in some other common location (such as in a folder on the C: drive)
  • Just have the application work with the copy of the file in the output (bin\Debug\) directory, i.e. the directory that the application will be installed to - you can change the properties of the item in your solution to have the item copied to the output directory, either all the time or only when the item in the solution directory is newer:

How to get the directory of database?

If you really want to use C:\wpf1\WpfApplication1\WpfApplication1 directory then the way to do it is to assume that this directory will always be 2 higher than the current working directory:

Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..\\.."));

This of course makes 2 horrible assumptions, first that the current directory is the directory that the .exe is contained in, and secondly that the directory you want is always 2 higher than this directory - I strongly recommend that you find another way as this sort of logic is highly unlikely to work on an end-users PC.

0

精彩评论

暂无评论...
验证码 换一张
取 消