简体   繁体   English

VB.Net:从项目中的数据文件获取文件路径

[英]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. 我正在创建一个原型,该模型将放入CD并移交给Visual Studio中的某个人。 In this prototype, I have multiple Excel spreadsheets that I use for data. 在这个原型中,我有多个用于数据的Excel电子表格。

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. 这将打开其中一个Excel电子表格。 This obviously wont work when I put it on a CD. 当我将其放在CD上时,这显然无法正常工作。 I have created a Folder in my Visual Studio Project ("Data") and put all data files I have in it. 我已经在我的Visual Studio项目(“数据”)中创建了一个文件夹,并将所有我拥有的数据文件放入其中。

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. 如果您已将名为“ Data”的文件夹添加到项目中,并将所有数据文件放在其中,则将其“ Build Action属性“ Content和“ Copy Local属性设置为Copy Always Copy If Newer ”或Copy If Newer ,则该“数据”文件夹将位于程序文件夹以及EXE。 In that case, assuming a Windows Forms application, you can use Application.StartupPath to get the root folder path: 在这种情况下,假设使用Windows Forms应用程序,则可以使用Application.StartupPath来获取根文件夹路径:

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. 这将在调试时或在最终发行版中起作用,因为它始终相对于program文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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