Wie kann man die lokalen Verzeichnisse eines Webservers als WebHostEnvironment in einer statischen Klasse zur Verfügung stellen oder abrufen.
Anleitung:
Hierzu muss man eine public static class MyServer mit einer statischen Eigenschaft wie WebHostEnvironment erstellen. Diese Eigenschaft oder Variable muss im Startup eingestellt werde und steht dann immer zur Verfügung
Erstellen einer Static Class mit der WebHostEnvironment
using Microsoft.AspNetCore.Hosting;
public static class MyServer { public static IWebHostEnvironment WebHostEnvironment { get; set; }
}
|
Startup.cs erweitern
Damit die Umgebungsvariable zur Laufzeit zur Verfügung steht, muss die IWebHostEnvironment im Startup->Configure(..) übergeben werden.
Startup.cs mit Environment Variable
Hier die IWebHostEnvironment env direkt beim Aufruf als DependencyInjection übergeben.
public Startup(IConfiguration configuration, IWebHostEnvironment env) { Configuration = configuration; _env = env; }
public IConfiguration Configuration { get; } private readonly IWebHostEnvironment _env; |
Server Verzeichnis abfragen
*In einer statischen Klasse
Anschließend kann aus jeder Klasse heraus die allgemeine Umgebung abgefragt werden wie hier
string sPath_FFMpegDir = MyServer.WebHostEnvironment.ContentRootPath + @"\bin\FFMpeg\"; |
using System.Diagnostics; //ProcessStartInfo
public static class Video_Methods { public static void Video_create_Thumbnail(string sInputFile, string sOutputFile, string sPath_FFMpeg) { //---------------< Video_create_Thumbnail() >--------------- string sPath_FFMpegDir = MyServer.WebHostEnvironment.ContentRootPath + @"\bin\FFMpeg\"; var startInfo = new ProcessStartInfo { FileName = sPath_FFMpegDir + $"ffmpeg.exe", Arguments = $"-i " + sInputFile + " -an -vf scale=200x112 " + sOutputFile + " -ss 00:00:00 -vframes 1", CreateNoWindow=true, UseShellExecute=false, WorkingDirectory=sPath_FFMpegDir };
using (var process=new Process { StartInfo=startInfo}) { process.Start(); process.WaitForExit(); } //---------------</ Video_create_Thumbnail() >--------------- }
} |
Standard Startup.cs ohne Hosting Environment
public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; } |