How to get local file system path in azure websites

后端 未结 5 642
闹比i
闹比i 2020-12-31 20:25

I have a file hosted on the disk along with my website that I want to read .Not sure how do I access the file when I use System.Environment.CurrentDirectory it point to a D

相关标签:
5条回答
  • 2020-12-31 20:28

    I'm currently using AppDomain.CurrentDomain.BaseDirectory (.NET Core project). It returns "D:\home\site\wwwroot\" in Azure and the application root in local so the only difference is adding "bin\\" when it is Azure. I am searching the entire directory tree, just in case, but it can be trimmed.

    It's something like:

    private static string GetDriverPath(ILogger logger, string fileName)
    {
        var path = AppDomain.CurrentDomain.BaseDirectory;
    
        if (File.Exists(Path.Combine(path, fileName)))
        {
            return path;
        }
    
        string[] paths= Directory.GetFiles(path, fileName, SearchOption.AllDirectories);
    
        if (paths.Any())
        {
            return Path.GetDirectoryName(paths.First());
        }
    
        throw new FileNotFoundException($"{fileName} was not found in {path}.", fileName);
    }
    

    I'm new answering questions and this is an old one but I hope it helps someone.

    0 讨论(0)
  • 2020-12-31 20:38

    For testability, I use below code.

    string path = "";
    if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
        path = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot\\bin";
    else
        path = ".";
    path += "\\Resources\\myfile.json";
    

    In above example, I added myfile.json file to Resources folder in a project with Content and Copy if newer property setting.

    0 讨论(0)
  • 2020-12-31 20:46

    This works for me in both localhost and azure:

    Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "file_at_root.txt");
    

    System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath is the full local path to your site's root.

    0 讨论(0)
  • 2020-12-31 20:53

    You can do it using the below code.

    string fullFilePath = Environment.GetEnvironmentVariable("HOME") != null
        ? Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot\test.txt" //It will give the file directory path post azure deployment
        : Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\test.txt";//It will give the file directory path in dev environment.
    
    0 讨论(0)
  • 2020-12-31 20:54

    There is an environment variable called HOME in your website's environment that will get you part way there.

    You can access it using razor syntax or in code (C#). For example, suppose you have a file called data.txt that is at the root of your site with the default document and the rest of your files. You could get it's full path like this.

    @{ var dataFileName = Environment.GetEnvironmentVariable("HOME").ToString() + "\\site\\wwwroot\\data.txt"; }
    

    You can find this out on your own using the Site Control Management/"Kudu". For example, if your website is contoso.azurewebsites.net, then simply navigate to contoso.scm.azurewebsites.net. In here you can learn all about the file system and environment variables available to your website.

    0 讨论(0)
提交回复
热议问题