Is it possible to read File from same folder where Azure function exists
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, "..\\twinkle.txt"))
// Bin folder
Path.GetFullPath(Path.Combine(context.FunctionDirectory, "..\\bin\\twinkle.txt"))
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