We help IT Professionals succeed at work.

How to get application path in VB.NET ?

milani_lucie
milani_lucie asked
on
2,351 Views
Last Modified: 2012-06-27
Hi,

I have an XML file in my application root directory. I want to read that file. The following commands gives me .../Bin/Debug also. But i do not need that.

System.Environment.CurrentDirectory()
Application.StartupPath()

If my application directory is this : "C:\Test", then i do not want "C:\Test\Bin\Debug". I do need only "C:\Test"

Can you  please provide me sample code ?

Thanks
Comment
Watch Question

AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Try this

    Public Function App_Path() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Or

Application.Info.DirectoryPath

Hope this helps

Ashok

Author

Commented:
Still i am getting "Bin\Debug".

Thanks
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
If above does not work, try

Application.ExecutablePath

Ashok

Author

Commented:
Application.Info.DirectoryPath

is not there.

Thanks
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Also, make sure you are not running your application from "Bin\Debug".

If you check "Bin\Debug", does it have your application?  If so, you might be running your application from this location.

Ashok

Author

Commented:
The following is giving the complete path to EXE file including FileName.

Application.ExecutablePath

Thanks

Author

Commented:
My XML file is there in the root directory of the application. So i can't able to read it until i have the path something like "C:\Test\Sample.xml". I can't read if it is "C:\Test\Bin\Debug\Sample.xml" until i have this XML file over there in that location.

Thanks
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Copy your application in "C:\Test\" where you also have Sample.xml file.
Then run application from this location.

HTH

Author

Commented:
My application root directory is "C:Test" and the XML file is in "C:\Test\Sample.xml" location. To read this file, i need  to get the application root path which is "C:\Test" but not "C:\Test\Bin\Debug". All the commands you have provided are giving "Bin\Debug" path.

Thanks
CERTIFIED EXPERT

Commented:
milani_lucie,

The reason all the above options are returning Bin/Debug is because you are running the application in Debug mode. If you copy the application files to C:\Test\, and run the exe from there, it will work as expected.

Wayne
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Are you running your application from Visual Studio?

If so, run your application by double-clicking on your application EXE after opening C:\Test| folder.

HTH

Ashok

Author

Commented:
Here is the good article:

http://discuss.joelonsoftware.com/default.asp?dotnet.12.422983.5

I have followed that to get the "C:\Test" path instead of "C:\Test\Bin\Debug" path.

Thanks
CERTIFIED EXPERT

Commented:
Yes, but that doesn't change the fact that you are getting the "Bin\Debug" path because you are running your application in Debug mode from VS. When you deploy your application, it will not be run from "Bin\Debug", so the above suggestions will work perfectly.

Wayne
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I think so this is the good solution:

Application.StartupPath.replace("bin\Debug", "")

Thanks
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Final version!

        Dim strList As List(Of String) = New List(Of String)
        Dim strDupRec As List(Of String) = New List(Of String)
        Dim intDupRec As List(Of Integer) = New List(Of Integer)
        Dim intRecPos As List(Of Integer) = New List(Of Integer)
        Dim strOrigList As List(Of String) = New List(Of String)

        Dim intCount As Integer
        Dim intCount2 As Integer
        Dim strPrev As String = ""
        Dim bDupFound As Boolean = False

        strList.Add("EAAAAB")
        strList.Add("GAAAAB")
        strList.Add("CAAAAB")
        strList.Add("EAAAAB")
        strList.Add("AAAAAB")
        strList.Add("BAAAAB")
        strList.Add("CAAAAB")
        strList.Add("DAAAAB")
        strList.Add("CAAAAB")
        For intCount = 0 To strList.Count - 1
            strOrigList.Add(strList(intCount))
        Next

        strList.Sort()  ' This line is very important.

        For intCount = 0 To strList.Count - 1
            If strPrev <> "" Then
                If strPrev = strList(intCount) Then
                    If (strDupRec.IndexOf(strPrev) < 0) Then
                        strDupRec.Add(strPrev)
                        bDupFound = True
                        intDupRec.Add(2)
                        intRecPos.Add(intCount - 1)
                        intRecPos.Add(intCount)
                    Else
                        intDupRec(intDupRec.Count - 1) += 1
                        intRecPos.Add(intCount)
                    End If
                End If
            End If
            strPrev = strList(intCount)
        Next
        If bDupFound Then
            For intCount = 0 To strDupRec.Count - 1
                MessageBox.Show("Duplicate found with value = " + strDupRec(intCount) + ", Count = " + intDupRec(intCount).ToString)
            Next
            For intCount = 0 To intRecPos.Count - 1
                MessageBox.Show("Position = " + intRecPos(intCount).ToString)
            Next
            For intCount = 0 To intDupRec.Count - 1
                For intCount2 = 0 To strOrigList.Count - 1
                    If strDupRec(intCount) = strOrigList(intCount2) Then
                        MessageBox.Show("Original Position = " + intCount2.ToString)
                    End If
                Next
            Next
        End If

Note: Position is ZERO-BASED, if you want it ONE-BASED, just add 1, for example.....
          MessageBox.Show("Original Position = " + (intCount2 + 1).ToString)

HTH
Ashok
AshokSr. Software Engineer
CERTIFIED EXPERT

Commented:
Posted in wrong place

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.