开发者

How to load external data files without defining path explicitly?

I have a C# assembly that loads external files. This assembly is used by asp.net website. The following is the code that loads the data files

string dataDirectory = Pa开发者_运维百科th.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"data");
filePath = Path.Combine(dataDirectory,_fileName);

The component works fine in a windows app, but when I tried to use it in an asp.net website, it fails as the site could not find the data files, and it gives me the following error:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\cmictranslatorsite\20a8eddd\864b2575\assembly\dl3\5bd4a35e\8c6f79b6_98eccb01\data\file.txt


You need to use Server.MapPath to resolve relative physical paths in ASP.NET applications.

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) is returning C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\cmictranslatorsite\20a8eddd\864b2575\assembly\dl3\5bd4a35e\8c6f79b6_98eccb01\, which is the correct place where your application files copied and executed from.

If you want a directory relative to your Virtual Directory in IIS you use Server.MapPath to get the paths. If your data path is not relative perhaps .config file is the place to store this path. In either cases check the app pool identity or your authenticated users have appropriate access to the path you are reading from or writing to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜