如何获得在c#中开发的应用程序的相对根路径。净MVC吗?

时间:2021-09-15 04:09:26

I have a few unit tests that require files from the project to be used to run the unit tests. These files are just images. I need to get the image file using some kind of function within c#, other than pasting the full path like below.

我有一些单元测试,它们要求项目中的文件用于运行单元测试。这些文件只是图像。我需要在c#中使用某种函数来获取图像文件,而不是像下面这样粘贴完整的路径。

string filePath = @"C:\Users\user1\Documents\Visual Studio 2008\Projects\app1\app1.Tests\Fakes\test_files\test-image.jpg";

I will prefer to do something like:

我更喜欢做以下事情:

string filePath = app.path + "\Fakes\test_files\test-images.jpg"

How can I do that?

我怎么做呢?

Thanks!

谢谢!

3 个解决方案

#1


2  

If your question is about getting these files at runtime... you don't want to do this. You should be deploying these files somehow as part of your build process so that they end up in a predictable location, relative to your compiled binaries and other content.

如果你的问题是在运行时获取这些文件……你不想这么做。您应该以某种方式将这些文件作为构建过程的一部分进行部署,以便它们相对于编译的二进制文件和其他内容最终到达可预测的位置。

In Visual Studio, you can set any project file's Copy to Output Directory property to have it place the file in your output folder.

在Visual Studio中,您可以将任何项目文件的副本设置为Output Directory属性,使其将文件放在您的输出文件夹中。

Copy to Output Directory screenshot http://i33.tinypic.com/aky4j7.png

复制到输出目录屏幕截图http://i33.tinypic.com/aky4j7.png


Since it sounds like you want to get these files as part of your unit tests, you would want to use something like the MSTest attribute [DeploymentItem] instead. This will place the files in your test directory at runtime. Read about Test Deployment on MSDN.

因为这听起来像是要把这些文件作为单元测试的一部分,所以您需要使用MSTest属性(deploytem)之类的东西。这将在运行时将文件放在测试目录中。阅读MSDN上的测试部署。

#2


0  

Just to be clear, this would not be a unit test. A unit test should not rely on the environment. Anything in a database, a file system, etc. should not be used. A unit test should only be used to test a single unit of code. This is due to the fact that if anyone pulls down the source and runs the unit tests (the first thing you should do after getting the source) it will not run. They have to have the environment set up before the tests will pass. These are referred to as integration tests.

明确地说,这不是单元测试。单元测试不应该依赖于环境。不应该使用数据库、文件系统等中的任何内容。单元测试应该只用于测试单个代码单元。这是因为如果有人拉下源代码并运行单元测试(获取源代码后应该做的第一件事),它将不会运行。他们必须在测试通过之前设置好环境。这些被称为集成测试。

That being said, you could set up a variable if it is just in one class or an app.config if it is throughout multiple classes. You could then add a path in there as the default path and use that as "app.path".

也就是说,如果变量只是在一个类中,那么可以设置变量;如果变量是在多个类中,则可以设置为app.config。然后可以在其中添加一个路径作为默认路径,并将其用作“app.path”。

#3


0  

I dislike the above top-rated answer, because it doesn't answer the original question.

我不喜欢上面的答案,因为它没有回答原来的问题。

There are legitimate reasons to want to obtain the root path of an ASP.NET webapp, including MVC variety. For example, I need it so that I can pass in a 'URI pattern' which an XSL transform later uses to resolve a dynamically served XML document (using Xpath document function).

有合理的理由希望获得ASP的根路径。NET webapp,包括MVC多样性。例如,我需要它,这样我就可以传入一个“URI模式”,XSL转换稍后使用该模式解析动态服务的XML文档(使用Xpath文档函数)。

For the complete URI, you can perform a string operation on this.Request.Url.AbsoluteUri from within your controller/page.

对于完整的URI,可以对this.Request.Url执行字符串操作。来自您的控制器/页面中的绝对uri。

Use this.Request.Path for the path to current or this.Request.ApplicationPAth for the root path of the application.

使用this.Request。到当前或this.Request的路径。应用程序的根路径的ApplicationPAth。

#1


2  

If your question is about getting these files at runtime... you don't want to do this. You should be deploying these files somehow as part of your build process so that they end up in a predictable location, relative to your compiled binaries and other content.

如果你的问题是在运行时获取这些文件……你不想这么做。您应该以某种方式将这些文件作为构建过程的一部分进行部署,以便它们相对于编译的二进制文件和其他内容最终到达可预测的位置。

In Visual Studio, you can set any project file's Copy to Output Directory property to have it place the file in your output folder.

在Visual Studio中,您可以将任何项目文件的副本设置为Output Directory属性,使其将文件放在您的输出文件夹中。

Copy to Output Directory screenshot http://i33.tinypic.com/aky4j7.png

复制到输出目录屏幕截图http://i33.tinypic.com/aky4j7.png


Since it sounds like you want to get these files as part of your unit tests, you would want to use something like the MSTest attribute [DeploymentItem] instead. This will place the files in your test directory at runtime. Read about Test Deployment on MSDN.

因为这听起来像是要把这些文件作为单元测试的一部分,所以您需要使用MSTest属性(deploytem)之类的东西。这将在运行时将文件放在测试目录中。阅读MSDN上的测试部署。

#2


0  

Just to be clear, this would not be a unit test. A unit test should not rely on the environment. Anything in a database, a file system, etc. should not be used. A unit test should only be used to test a single unit of code. This is due to the fact that if anyone pulls down the source and runs the unit tests (the first thing you should do after getting the source) it will not run. They have to have the environment set up before the tests will pass. These are referred to as integration tests.

明确地说,这不是单元测试。单元测试不应该依赖于环境。不应该使用数据库、文件系统等中的任何内容。单元测试应该只用于测试单个代码单元。这是因为如果有人拉下源代码并运行单元测试(获取源代码后应该做的第一件事),它将不会运行。他们必须在测试通过之前设置好环境。这些被称为集成测试。

That being said, you could set up a variable if it is just in one class or an app.config if it is throughout multiple classes. You could then add a path in there as the default path and use that as "app.path".

也就是说,如果变量只是在一个类中,那么可以设置变量;如果变量是在多个类中,则可以设置为app.config。然后可以在其中添加一个路径作为默认路径,并将其用作“app.path”。

#3


0  

I dislike the above top-rated answer, because it doesn't answer the original question.

我不喜欢上面的答案,因为它没有回答原来的问题。

There are legitimate reasons to want to obtain the root path of an ASP.NET webapp, including MVC variety. For example, I need it so that I can pass in a 'URI pattern' which an XSL transform later uses to resolve a dynamically served XML document (using Xpath document function).

有合理的理由希望获得ASP的根路径。NET webapp,包括MVC多样性。例如,我需要它,这样我就可以传入一个“URI模式”,XSL转换稍后使用该模式解析动态服务的XML文档(使用Xpath文档函数)。

For the complete URI, you can perform a string operation on this.Request.Url.AbsoluteUri from within your controller/page.

对于完整的URI,可以对this.Request.Url执行字符串操作。来自您的控制器/页面中的绝对uri。

Use this.Request.Path for the path to current or this.Request.ApplicationPAth for the root path of the application.

使用this.Request。到当前或this.Request的路径。应用程序的根路径的ApplicationPAth。