Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

How to provide or retrieve the local directories of a web server as WebHostEnvironment in a static class.

 

guide:

To do this, you need to create a public static class MyServer with a static property such as WebHostEnvironment. This property or variable must be set in the startup and is then always available

 

Create a Static Class with the WebHostEnvironment

 

using Microsoft.AspNetCore.Hosting;

 

public static class MyServer

{

         public static IWebHostEnvironment WebHostEnvironment { get; set; }

 

}

 

 

 

 

 

 

Extend Startup.cs

For the environment variable to be available at run time, the IWebHostEnvironment must be passed in the Startup >Configure(..).

 

Startup.cs with Environment Variable

Here the IWebHostEnvironment env is passed directly as DependencyInjection when called.

 

 

public Startup(IConfiguration configuration, IWebHostEnvironment env)

{

    Configuration = configuration;

    _env = env;

}

 

public IConfiguration Configuration { get; }

private readonly IWebHostEnvironment _env;

 

 

 

 

Query server directory

*In a static class

Then the general environment can be queried from each class as here

 

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 without Hosting Environment

public Startup(IConfiguration configuration)

{

    Configuration = configuration;

}

 

public IConfiguration Configuration { get; }

 

Mobile

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de