简体   繁体   English

如何在Visual Studio中使用C#在解决方案中轻松获取文件夹内文件的路径?

[英]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.

相关问题 我可以将 windows (.bat ) 文件放在 Visual Studio C# 项目的资源文件夹中吗? - Can I put a windows (.bat ) file inside the resources folder of a Visual studio C# project? 如何以编程方式获取有关其他解决方案的信息或进行操作? (Visual Studio,C#) - How can I programmatically get information about or manipulate another solution? (Visual Studio, C#) 如何获取当前Active Solution Platform名称以在Visual Studio加载项中使用? (C#) - How can I get the current Active Solution Platform name for use in a Visual Studio add-in? (C#) Visual Studio扩展:如何在选择时获取解决方案文件夹的“路径”? - Visual Studio Extension: How to get the “path” of a solution folder on selection? 如何在Visual Studio解决方案的项目中检查文件是否存在于文件夹中 - How to check if file exists in a folder in my project in Visual Studio solution 如何在visual studio 2013解决方案资源管理器中获取文件路径? - How to get a file path in visual studio 2013 solution explorer? 一旦我压缩文件(它们从 Visual Studio 中的文件路径引用打开),如何让我的应用程序从单独的 Winform 打开? (C#) - How do I get my app to open from a separate Winform once I ZIP the files (they open from a file path reference in Visual Studio)? (C#) 如何获取程序解决方案中文件夹的路径/地址? (C#) - How do I get the path/address of a folder within the program solution? (C#) 如何在运行时将 c# 文件添加到 Visual Studio 解决方案资源管理器? - How to add a c# file to Visual Studio Solution Explorer at runtime? 如何重构我的 C# 代码以使其易于测试? - How can I refactor my C# code to be easily testable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM