[英]How can I easily get the path of a file inside a folder in my solution in Visual Studio with C#?
I am using Visual Studio 2010 Professional. 我正在使用Visual Studio 2010专业版。
I have created a folder inside a project in my solution using the "right click -> Add -> New folder -> folder1"
method, and now my solution explorer looks like this: 我已经使用
"right click -> Add -> New folder -> folder1"
方法在解决方案的项目中创建了一个文件夹,现在我的解决方案资源管理器如下所示:
Is there a way for me to programmatically access files inside this folder (ex: sampleFile.xml) from code , other than get the path of the executable and go from there? 除了获取可执行文件的路径并从那里进入之外,我是否有办法通过代码以编程方式访问此文件夹中的文件(例如:sampleFile.xml)?
Edit: I want to know if there's a more elegant solution provided by VS/C#, I know I can construct the path using the executable path or base directory. 编辑:我想知道VS / C#是否提供了更优雅的解决方案,我知道我可以使用可执行路径或基本目录来构造路径。
For example, found this here : if I add the file as a resource (Right click on project -> Properties -> Resources -> Add Resource) a "Resource" folder is generated in my solution explorer : 例如,在这里找到它:如果我将文件添加为资源(右键单击项目->属性->资源->添加资源),则在我的解决方案资源管理器中会生成“资源”文件夹:
and I can get the content of the file easily like this : 这样我就可以轻松获取文件内容:
var newFile = Properties.Resources.newFile1;
Is there something similar for other non-resource files in the solution and can I get the path of the files in a similar way? 解决方案中的其他非资源文件是否有类似内容,我是否可以类似的方式获取文件的路径?
Assuming that Test
is your main project, then the file can be accessed via: 假设
Test
是您的主要项目,则可以通过以下方式访问文件:
AppDomain.CurrentDomain.BaseDirectory + @"\Folder1\SampleFile.xml"
(This assumes that: (a) you want to access it from within Visual Studio, or (b) that whatever install mechanism you use places Folder1
within the start directory of the application.) (这假定:(a)您想从Visual Studio中访问它,或(b)您使用的任何安装机制都将
Folder1
放在应用程序的开始目录中。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.