在C#中实现文件访问的最佳方式

Mic*_*ern 3 c# wcf coding-style file

场景 - 我需要访问HTML模板以从我的业务逻辑层生成电子邮件.它是一个类库,包含一个包含该文件的子文件夹.当我在单元测试中尝试以下代码时:

string FilePath = string.Format(@"{0}\templates\MyFile.htm", Environment.CurrentDirectory);
string FilePath1 = string.Format(@"{0}\templates\MyFile.htm", System.AppDomain.CurrentDomain.BaseDirectory);
Run Code Online (Sandbox Code Playgroud)

它使用的是C:\ WINNT\system32 \或ASP.NET临时文件夹目录.

在不使用app.config或web.config文件的情况下访问此文件的最佳方法是什么?

[这是使用WCF服务]

swi*_*ams 6

你是从ASP.Net应用程序运行的吗?请Server.MapPath()改用.

还要看一下System.IO.Path.Combine()连接路径.

[编辑]由于你不能使用System.Web,试试这个:

System.Reflection.Assembly.GetExecutingAssembly().Location
Run Code Online (Sandbox Code Playgroud)

或者GetEntryAssembly().