场景 - 我需要访问 HTML 模板以从我的业务逻辑层生成电子邮件。它是一个类库,包含一个包含文件的子文件夹。当我在单元测试中尝试以下代码时:
string FilePath = string.Format(@"{0}\templates\MyFile.htm", Environment.CurrentDirectory);
string FilePath1 = string.Format(@"{0}\templates\MyFile.htm", System.AppDomain.CurrentDomain.BaseDirectory);
它正在使用 C:\WINNT\system32\或 ASP.NET 临时文件夹目录。
在不使用 app.config 或 web.config 文件的情况下访问此文件的最佳方法是什么?
[这是使用 WCF 服务]
最佳答案
您是从 ASP.Net 应用程序运行它的,对吗?请改用 Server.MapPath()
。
另请查看用于连接路径的 System.IO.Path.Combine()
。
[编辑]
由于您不能使用 System.Web
,请尝试以下操作:
System.Reflection.Assembly.GetExecutingAssembly().Location
或GetEntryAssembly()
。
关于c# - 在 C# 中实现文件访问的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/234976/