在 Azure WebJob 中访问 C# 控制台应用程序的文件路径
Access filepath of C# Console Application in Azure WebJob
我的 C# 控制台项目“~/Assets/Samle.xlsx”下有文件夹。我想在本地计算机和 Azure WebJob 上访问此 excel 文件。请帮助
对于您的本地端,如果文件在 ~/Assets/Samle.xlsx
下,请确保您已将 复制到输出目录 设置为 始终复制 为您的 Samle.xlsx
文件,那么您可以使用以下代码获取文件路径:
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\Samle.xlsx");
当作为 Azure WebJob 发布时,它会在您的网络应用程序的 D:\home\site\wwwroot\app_data\jobs\{job type}\{job name}
下,您可以使用 kudu to check it.Your webjob would be copied to a temp directory (e.g. %TEMP%\jobs\{job type}\{job name}\{random name}
) by default for running, at this point your excel file would under the temp folder. For more details, you could refer to WebJob Working Directory。
默认情况下,您正在读取临时文件夹下的 excel 文件。如果你想直接从 WebJob 二进制目录读取 excel 文件,你可以更改 WebJob Working Directory 或者你可以使用指向 d:\home\site\wwwroot
的 WEBROOT_PATH
环境变量,然后你可以将它与您的路径组合如下:
var path = $@"{Environment.GetEnvironmentVariable("WEBROOT_PATH")}\app_data\jobs\<job type>\<job name>\Assets\Samle.xlsx";
此外,要读取 excel 文件,您可以使用 NPOI, SpreadsheetLight, Aspose.Cells for .NET, etc. Additionally, you could store your resource(s) to Azure Blob storage as the central storage and you could refer to here 开始使用 Azure Blob 存储。
我的 C# 控制台项目“~/Assets/Samle.xlsx”下有文件夹。我想在本地计算机和 Azure WebJob 上访问此 excel 文件。请帮助
对于您的本地端,如果文件在 ~/Assets/Samle.xlsx
下,请确保您已将 复制到输出目录 设置为 始终复制 为您的 Samle.xlsx
文件,那么您可以使用以下代码获取文件路径:
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\Samle.xlsx");
当作为 Azure WebJob 发布时,它会在您的网络应用程序的 D:\home\site\wwwroot\app_data\jobs\{job type}\{job name}
下,您可以使用 kudu to check it.Your webjob would be copied to a temp directory (e.g. %TEMP%\jobs\{job type}\{job name}\{random name}
) by default for running, at this point your excel file would under the temp folder. For more details, you could refer to WebJob Working Directory。
默认情况下,您正在读取临时文件夹下的 excel 文件。如果你想直接从 WebJob 二进制目录读取 excel 文件,你可以更改 WebJob Working Directory 或者你可以使用指向 d:\home\site\wwwroot
的 WEBROOT_PATH
环境变量,然后你可以将它与您的路径组合如下:
var path = $@"{Environment.GetEnvironmentVariable("WEBROOT_PATH")}\app_data\jobs\<job type>\<job name>\Assets\Samle.xlsx";
此外,要读取 excel 文件,您可以使用 NPOI, SpreadsheetLight, Aspose.Cells for .NET, etc. Additionally, you could store your resource(s) to Azure Blob storage as the central storage and you could refer to here 开始使用 Azure Blob 存储。