Bob
Bob

Reputation: 1396

VB.Net: Getting File Path from Data Files In Project

I'm creating a prototype that I will be putting on a CD and handing off to someone in Visual Studio. In this prototype, I have multiple Excel spreadsheets that I use for data.

I have the following line of code:

dataWorkBook = dataWorkbookApp.Workbooks.Open("C:\Users\me\Desktop\Task\Prototype Data.xlsx")

Which opens one of the Excel Spreadsheets. This obviously wont work when I put it on a CD. I have created a Folder in my Visual Studio Project ("Data") and put all data files I have in it.

My questions is how do I get the file path to those files and put it in the above code?

Upvotes: 0

Views: 1917

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

If you have added a folder named "Data" to your project and put all your data files in there, then set their Build Action property Content and their Copy Local property to Copy Always or Copy If Newer, that "Data" folder will be in the program folder along with the EXE. In that case, assuming a Windows Forms application, you can use Application.StartupPath to get the root folder path:

dataWorkBook = dataWorkbookApp.Workbooks.Open(IO.Path.Combine(Application.StartupPath, "Data\Prototype Data.xlsx"))

That will work while debugging or in the final release, because it's always relative to the program folder.

Upvotes: 1

Related Questions