Necati
Necati

Reputation: 23

Reading files with Azure Function

Im trying to read a file that I've created which contains some URL's for my Azure Function app. I am using the File object to read it and eventually parse it to an array, but it cannot seem to find the file.

using (StreamReader r = File.OpenText(path))
        {
            Console.WriteLine(r.ToString());
        }

Error occurs when its trying to read the Path which I have passed through from the run function as seen below.

var path = System.IO.Path.Combine(executionContext.FunctionDirectory, "url.json");
getURLs(path);

I was told to use the ExecutionContext which I have applied now, but I still am not able to see the file that i've created. I am trying to run the function app locally. I have checked the bin/debug files and noticed that the URL.json file was not being copied over to the output files so I've also changed the property to copy always. enter image description here

What I have noticed is that the file is being deleted when I am running the application.

enter image description here

When manually building the application it does show that its being copied over from the project folder but when run it gets removed. Does anyone encountered this issue and know what I should do to fix this?

Upvotes: 0

Views: 4381

Answers (1)

SaiSakethGuduru
SaiSakethGuduru

Reputation: 2440

The below code will help you in reading the files

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 further information check the SO

Upvotes: 1

Related Questions