10

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 drive location .Can someone please tell me how can I get to my file stored at the root of where my site is hosted.

Thanks

sp9
  • 755
  • 3
  • 11
  • 22

5 Answers5

13

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.

Rick Rainey
  • 11,096
  • 4
  • 30
  • 48
  • 2
    I'm doing the same on Azure and I got "D:\home\site\wwwroot\bin\NewFolder\FileName.json", but I still cannot access... WHY!!?!?!? I know the file is there as I can see it on Kudu... What am I missing? – Danimal111 Feb 06 '17 at 19:23
5

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.

Youngjae
  • 24,352
  • 18
  • 113
  • 198
1

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.

Gordon Glas
  • 1,659
  • 2
  • 15
  • 23
  • 1
    FYI: Running this in Azure gave me : AppDomain.CurrentDomain.BaseDirectory='D:\Program Files (x86)\SiteExtensions\Functions\1.0.11090\' Which was not close to "D:\home\site\wwwroot\" – granadaCoder Aug 15 '17 at 19:35
  • @granadaCoder Thanks, It has worked for me, but I updated it with HostingEnvironment.ApplicationPhysicalPath, which should give you "D:\home\site\wwwroot\" in Azure. – Gordon Glas Aug 16 '17 at 17:00
  • It seems that HostingEnvironment.ApplicationPhysicalPath will give the path granadaCoder mentioned when running in Azure Functions. – Lukasz Mendakiewicz Sep 10 '18 at 21:42
1

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.

Dodger
  • 342
  • 3
  • 9
1

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.
Rajeev Kumar
  • 371
  • 2
  • 9