How to get local file system path in azure websites

后端 未结 5 666
闹比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.

提交回复
热议问题