是否可以从存在Azure功能的同一文件夹中读取文件 [英] Is it possible to read File from same folder where Azure function exists

查看:54
本文介绍了是否可以从存在Azure功能的同一文件夹中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Azure C#函数中,我需要读取一个.txt文件.我在Visual Studio中制作了.txt文件,并将其设置为始终复制".

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");

此代码无效.当我打开文件夹,它是dir的值.它将我引到该目录"C:\ Users {用户名} \ AppData \ Local \ Azure.Functions.Cli \ 1.0.9""

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""

如何在Azure函数中存储简单的txt文件.或者我需要Azure存储.

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.

更新以显示复制的文件

推荐答案

以下是获取正确文件夹的方法:

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");
    // ...
}

这将使您进入带有 function.json 文件的文件夹.如果需要进入 bin 文件夹,则可能需要上一层,然后附加 bin :

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"))

这篇关于是否可以从存在Azure功能的同一文件夹中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆