gpt4 book ai didi

c# - 是否可以从Azure功能所在的同一文件夹中读取文件

转载 作者:太空狗 更新时间:2023-10-29 18:08:05 26 4
gpt4 key购买 nike

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

现在我正在使用此代码来读取文件

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

如何在 Azure 函数中存储简单的 txt 文件。或者我需要 Azure 存储来实现此目的。

我还能做些什么来完成这件事。

更新显示已复制的文件

enter image description here

最佳答案

以下是如何到达正确的文件夹:

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

这将带您进入包含 function.json 文件的文件夹。如果您需要访问 bin 文件夹,您可能需要上一级,然后附加 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"))

关于c# - 是否可以从Azure功能所在的同一文件夹中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49597721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com