Best way to implement file access in C #

Scenario. I need to access an HTML template to create email from my business logic level. This class library contains a subfolder containing the file. When I tried the following code in unit test:

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

He used the C: \ WINNT \ system32 \ directory or the ASP.NET Temporary Folder directory.

What is the best way to access this file without using the app.config or web.config file?

[This is using the WCF service]

+3
source share
4 answers

Are you using this from an ASP.Net application? Use instead Server.MapPath().

Also consider System.IO.Path.Combine()for path concatenation.

[] System.Web, :

System.Reflection.Assembly.GetExecutingAssembly().Location

GetEntryAssembly().

+6

,

System.IO.Path.GetDirectoryName(Application.ExecutablePath);
+1

I got this job on my site! I have not worked on this for the past 2 years only.

I added this to my system.serviceModel block for web.config. It seems to be doing it all the way regarding where your service is installed.

  <system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">

better late than never?

+1
source

Using

System.Web.HttpServerUtility.MapPath( "~/templates/myfile.htm" )
0
source

Source: https://habr.com/ru/post/1698691/


All Articles