Sunny Bhattacharjee
Sunny Bhattacharjee

Reputation: 107

Getting physical path of file in project directory

I am using Environment.CurrentDirectory to get the current directory, i.e. "D:\Chuttu\Projects\LIC\bin\Debug"

But, I want to remove "\bin\Debug"

How can I do that? Or is there any way so that I can get the directory up to "LIC\"?

Actually this is my connection string:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

But when I use this and try to insert data into the DB, the data is not inserted. When I use the connection string with a physical path it starts working:

Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

I am trying to get the physical path of the mdf file and use it in the connection string.


EDIT:

This piece of code works fine:

Private Sub save()
        Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Chuttu VB\Projects\LIC\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

As soon as I change the connection string to the connection string from app.config it stops working (adding data to DB):

Private Sub save()
        Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LIC.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

        Dim sql As New SqlClient.SqlCommand("INSERT INTO ProposerDetails " & _
                                            "VALUES (" & Convert.ToInt32(PolicyNumberTextBox.Text) & ",'" & NameTextBox.Text & "','" & AgeTextBox.Text & "','" & PhoneTextBox.Text & "','" & AddressTextBox.Text & "','" _
                                            & NomineeTextBox.Text & "','" & NomineeRelationTextBox.Text & "'," & PlanID() & ",'" & PolicyTermTextBox.Text & "','" & PremiumAmountTextBox.Text & "','" _
                                            & PremiumTypeComboBox.Text & "','" & SumProposedTextBox.Text & "','Date' )", con)

        MsgBox(sql.CommandText)
        con.Open()

        MsgBox(con.State.ToString)
        Dim i As Integer = sql.ExecuteNonQuery
        MsgBox(i.ToString)
        con.Close()
        sql.Dispose()
        con.Dispose()
        ToolStripStatusLabelMessage.Text = "Saved"
    End Sub

NOTE: I get no errors.

Upvotes: 0

Views: 15404

Answers (5)

this is to answer your question about the current path or directory of your project. You can use the REPLACE function to remove the "\bin\Debug\" part like this:

Dim path As String path = My.Application.Info.DirectoryPath Dim path1 As String = path.Replace("\bin\Debug", "\")

Upvotes: 0

cadrell0
cadrell0

Reputation: 17337

You don't have an issue with your connection string. The issue is that every time you run your application, it uses a fresh copy of your mdf that is included in your project. Take a look at this blog post about the problem.

Upvotes: 0

Tom Cerul
Tom Cerul

Reputation: 1953

Are you sure you're going to want your final executable to navigate up two directories to find its database? Also, newer versions of Windows are demanding admin privledges to write to you applications own folder in c:\Program Files. Apparently, they want you storing stuff in c:\ProgramData or so.

Upvotes: 2

Matthew
Matthew

Reputation: 25793

Go to properties on your MDF file within visual studio (assuming you have it added as part of the project). Set the "Copy to Output Directory" to "Copy if newer". See http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx for more information.

EDIT: Just to clarify that this does not get your base directory, it just makes sure that your database ends up in the folder with your executable, so relative paths will work.

Upvotes: 1

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107616

You could try:

string dataDirectory = AppDomain.CurrentDomain.BaseDirectory;

Reference:

Upvotes: 3

Related Questions