31

In my Azure C# function I need to read a .txt file. I make the .txt file in Visual studio and set it to "copy Always".

Now I am using this code to read the file

var dir = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);

var path = System.IO.Path.Combine(dir, "twinkle.txt");

this code doesn't work. When I open the folder which is the value of dir. It lead me to this directory ""C:\Users{username}\AppData\Local\Azure.Functions.Cli\1.0.9""

How I can store a simple txt file in Azure function. Or I need Azure Storage for this.

Anything else I can do to get this done.

Update for showing file copied

enter image description here

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79

4 Answers4

68

Here is how to get to the correct folder:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var path = System.IO.Path.Combine(context.FunctionDirectory, "twinkle.txt");
    // ...
}

This gets you to the folder with function.json file. If you need to get to bin folder, you probably need to go 1 level up, and then append bin:

// One level up
Path.GetFullPath(Path.Combine(context.FunctionDirectory, $"..{Path.DirectorySeparatorChar}twinkle.txt"))

// Bin folder
Path.GetFullPath(Path.Combine(context.FunctionDirectory, $"..{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}twinkle.txt"))
Community
  • 1
  • 1
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
34

For those like me who doesn't have access to ExecutionContext since we have to read a file in Startup.

var binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var rootDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));

///then you can read the file as you would expect yew!
File.ReadAllText(rootDirectory + "/path/to/file.ext");

Also worth noting that Environment.CurrentDirectory might work on a local environment, but will not work when deployed to Azure.

Works inside functions too.

Reference

Louie Almeda
  • 5,366
  • 30
  • 38
  • 2
    Super useful for including Google.json files that are necessary for FirebaseAdmin functions! Thanks, louie! – Bill Noel Feb 19 '20 at 18:31
  • Wondering how this has worked for everyone. The executing assembly directory that I see is a temporary ASP.NET location and the config file of course does not exists there. "D:\local\Temporary ASP.NET Files\root\80e1a0b8\1c033853\assembly\dl3\dc0956b6\". – Prasad Korhale Oct 12 '20 at 22:42
  • Hmmm, that is strange indeed, it's maybe worth checking out the reference I included in the answer @PrasadKorhale – Louie Almeda Oct 14 '20 at 12:39
  • If I have the file in folder it doesn't work, it had to be directly places under root folder – lolelo Mar 22 '21 at 13:24
  • @PrasadKorhale, because I think you are deploying the source code (as opposed to a built assembly) and your project is being compiled on the hosting of Functions App. – Reza Mar 01 '22 at 17:33
  • @lolelo then you have to navigate to the folder after getting the root directory similar to what I have in the answer `rootDirectory + "/your/sub/folders/file.ext"` – Louie Almeda Mar 23 '22 at 16:59
  • Needed to add "wwwroot" to the end for working. var rootDirectory = Path.Combine(Path.GetFullPath(Path.Combine(binDirectory, ".."),"wwwroot")); – Ivan Valadares Jun 29 '23 at 02:07
-1

Here is a useful link: https://github.com/Azure/azure-functions-host/wiki/Retrieving-information-about-the-currently-running-function

There is the possibility of a hard coded way:

File.ReadAllText("d:\home\site\wwwroot\NameOfYourFunction" + "/path/to/file.ext");
-1

To use a file called pub_key.pem in an Azure Function. I'd rather do this in the .csproj file:

<ItemGroup>
    <EmbeddedResource Include="pub_key.pem">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
</ItemGroup>
 

and then read the file using a Stream

var assy = Assembly.GetAssembly(typeof(AnyClassInFunction)); 
Stream fileStream = assy.GetManifestResourceStream(typeof(AnyClassInFunction),"pub_key.pem");

Hamed
  • 356
  • 2
  • 9