对于asp.net中的web应用程序,我必须打开位于我的"app_data“目录中的一个文件。在本地,它工作得很好。
但是当发布到服务器上时,我会出现一个错误:“系统找不到指定的文件”
下面是我的代码:
StreamReader reader = new streamReader(Server.MapPath("~/App_Data/pubkey.pem"));
当然,该文件存在于服务器上。
我做错了什么?
谢谢;)
编辑:由于错误不在我认为她所在的位置,我的问题就解决了。我已经将mike的回答标记为有效,因为它对我发现自己错了有很大帮助:)
发布于 2018-06-08 05:32:05
您需要一个在IIS中定义为虚拟文件夹的App_Data文件夹,或者在您的网站项目中需要一个名为App_Data的文件夹,因为~表示转到website的根目录。如果您在windows的用户配置文件中查找App_Data文件夹,则是虚拟文件夹问题,或者/并且为应用程序池标识提供了访问该文件夹的适当权限。
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(Server.MapPath("~/App_Data/pubkey.pem")))
{
// Read the stream to a string, and write the string to the console.
string doc = sr.ReadToEnd();
System.Diagnostics.Debug.WriteLine(doc);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("The file could not be read:");
System.Diagnostics.Debug.WriteLine(e.Message);
}
https://stackoverflow.com/questions/50748699
复制相似问题