简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM