简体   繁体   English

C#中的相对路径

[英]Relative paths in C#

I need some help with paths please! 我需要一些路径的帮助!

Basically I have a project with the following folder structure: 基本上我有一个具有以下文件夹结构的项目:

Project (root directory which contains the .sln file etc.)
Project/MyProj (contains the code)
Project/MyProjTest (the test folder)
Project/TestResults

Now with this Project I need to have a common folder where I can stick a bunch of files for use with the Application without having to copy the files to multiple locations etc. What is the best way to do this? 现在使用这个项目我需要有一个公共文件夹,我可以在其中粘贴一堆文件以便与应用程序一起使用,而无需将文件复制到多个位置等。这样做的最佳方法是什么? Ideally I would like to have the folder as Project/ResourcesFolder, so that both the Code folder and Test folder can access it. 理想情况下,我希望将文件夹作为Project / ResourcesFolder,以便Code文件夹和Test文件夹都可以访问它。 Now if this is the case how do I call this folder from within C#? 现在,如果是这种情况,我如何在C#中调用此文件夹? I've tried Application.StartupPath, Environment.GetCurrentDirectory but they both just return the CURRENT folder which is not what I want. 我已经尝试过Application.StartupPath,Environment.GetCurrentDirectory,但它们都只返回了CURRENT文件夹,这不是我想要的。

Thanks in advance. 提前致谢。

您可以将解决方案文件夹添加到解决方案中,并将常用文件放入其中。

You'll have to copy the files, you'll want your program to operate the same way after it is deployed. 您必须复制文件,您希望程序在部署后以相同的方式运行。 The simplest way to do so is by adding them to your project. 最简单的方法是将它们添加到项目中。 In the Properties window, set Build Action = None, Copy to Output Directory = Copy if Newer. 在“属性”窗口中,设置“构建操作=无”,“复制到输出目录”=“如果较新则复制”。 The latter setting ensures that you don't waste time copying the files over and over again. 后一种设置可确保您不会浪费时间一遍又一遍地复制文件。

This ensures that the files will be present in the same directory as your EXE. 这可确保文件与EXE位于同一目录中。 Both when you debug and after you deploy it. 在调试和部署之后。 Simply use Application.StartupPath to locate them. 只需使用Application.StartupPath即可找到它们。 Creating the Setup project for the app is now very simple as well. 现在,为应用程序创建安装项目也非常简单。

Note that if the files are small you really want to embed them as resources. 请注意,如果文件很小,您确实希望将它们嵌入为资源。

.. goes one directory up. ..进入一个目录。 That is, from Project/MyProjTest you could access Project/MyProj via ../MyProj . 也就是说,从Project/MyProjTest您可以通过../MyProj访问Project/MyProj

Use Server.MapPath("~") to get to the root folder of your application. 使用Server.MapPath(“〜”)转到应用程序的根文件夹。 From there you can get to wherever you need. 从那里你可以到达你需要的任何地方。

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

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