当前位置: 动力学知识库 > 问答 > 编程问答 >

.net - Application.StartupPath pointing to strange MS Directory

问题描述:

strange error. One of which i cannot find any existing resources to solve the issue.

The issue is, when using Application.StartupPath I get pointed to a folder at "C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0". Naturally, this breaks everything which depends on the correct folder (the one my solution is actually in) being given.

What is more strange is that there exists a unit-test to make sure that this path at least has the correct root folder and sometimes it will pass and Application.StartupPath will be working as expected, and then the very next debug, will point to the aforementioned folder again. Very confusing and very annoying !

Any ideas ?

网友答案:

It seems like you have web application and this is why it spins up the web server which is the actual application which reports StartupPath. Your unit tests probably run as a console application or something similar which results in a different StartupPath. For web applications you'd better use Server.MapPath to deal with file system paths.

网友答案:

For those of you that may come across this thread later and feel this would be a solution to your issue, There are a few things I feel are worth a mention.

  1. In order to use Server.MapPath() in a class library project or a project that does not directly handle 'views', you will need to add a reference to System.Web

  2. Sadly, I cannot find the exact post but in trying to find an answer to this question, I came across another post (which didn't answer my question) mentioning some alternate scenario (one that I didn't understand much) where simply using Server.MapPath would still cause errors. This same post suggested that instead, HttpContext.Current.Server.MapPath() was used.

分享给朋友:
您可能感兴趣的文章:
随机阅读: