-2

I'm trying to get the Base path of my VB Project dynamically since the path that the project is run from will change.

Let's say that the VB Project file is in C:\MyVBProjects\May2016\MyProject\MyApp.vbproj

I tried using Application.ExecutablePath, Assembly.GetExecutingAssembly().Location and Application.StartupPath

The above three return C:\MyVBProjects\May2016\MyProject\bin\Debug\

I also tried using the following code:

Curr = Directory.GetCurrentDirectory()
Root = Directory.GetRootDirectory(Curr)

This returns just C:\

Is there anyway I can get it to say C:\MyVBProjects\May2016\MyProject\ ?

I'm currently using Visual Basic 2012 on Windows 7.

Buzz Lightyear
  • 824
  • 1
  • 7
  • 18
  • 2
    Why does the exe need to know the location of the .sln or .proj file anyway? – Matt Wilko May 09 '16 at 15:18
  • Hey @MattWilko are you suggesting using `Directory.GetParent(Directory.GetParent(Application.ExecutablePath))`? Would that always return the same value though? – Buzz Lightyear May 09 '16 at 15:21
  • 2
    See [this](http://stackoverflow.com/q/1642827/1070452) and [this](http://stackoverflow.com/q/1222190/1070452) among many others... (somewhere there is a really good explanation - I think by Reed Copsey; I'll leave you to use your google-fu to find it). You may well *not* have permission to save files to the EXE location depending on how/where it is installed. – Ňɏssa Pøngjǣrdenlarp May 09 '16 at 15:27
  • Hey @Plutonix I read those two links before posting the question. All of them return C:\MyVBProjects\May2016\MyProject\bin\Debug\ or with the .exe. Also, I need to edit my comment. That's a typo. I meant it opens files (.sql and .txt) in the .vbproj location. – Buzz Lightyear May 09 '16 at 15:50
  • Googling to find Reed Copsey's explanation. – Buzz Lightyear May 09 '16 at 15:52
  • 1
    Of course it will return a VS related folder when run in debug from VS. But that is not where you should be storing data anyway - you often will not have rights to write to the EXE folder at runtime. Store data in an AppData Folder (`Users\...`) that is what they are for, – Ňɏssa Pøngjǣrdenlarp May 09 '16 at 16:07
  • 1
    `Path.GetDirectoryName(Path.GetDirectoryName(Application.ExecutablePath))`, and I'll re-ask Matt's question: Why do you even need this? If you publish (or just move) your application you won't have access to that folder anymore. – Visual Vincent May 09 '16 at 16:08

1 Answers1

0

Works in design time :

    Dim DTE As EnvDTE.DTE = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE")
    Dim Prj As Project = DTE.Solution.Projects.Item(1)
    Dim PrjFile As String = Prj.FullName

You should reference "envdte" (in Assemblys -> extensions) and imports envDTE.

Marc F
  • 33
  • 5