gpt4 book ai didi

c# - 我可以从服务器上运行的 Web API 应用程序将文件写入服务器计算机上的文件夹吗?

转载 作者:太空狗 更新时间:2023-10-29 17:36:44 25 4
gpt4 key购买 nike

我的 Web API 应用程序中有这段代码可以写入 CSV 文件:

private void SaveToCSV(InventoryItem invItem, string dbContext)
{
string csvHeader = "id,pack_size,description,vendor_id,department,subdepartment,unit_cost,unit_list,open_qty,UPC_code,UPC_pack_size,vendor_item,crv_id";

int dbContextAsInt = 0;
int.TryParse(dbContext, out dbContextAsInt);
string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

string csv = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", invItem.ID,
invItem.pksize, invItem.Description, invItem.vendor_id, invItem.dept, invItem.subdept, invItem.UnitCost,
invItem.UnitList, invItem.OpenQty, invItem.UPC, invItem.upc_pack_size, invItem.vendor_item, invItem.crv_id);

string existingContents;
using (StreamReader sr = new StreamReader(csvFilename))
{
existingContents = sr.ReadToEnd();
}

using (StreamWriter writetext = File.AppendText(csvFilename))
{
if (!existingContents.Contains(csvHeader))
{
writetext.WriteLine(csvHeader);
}
writetext.WriteLine(csv);
}
}

在开发机器上,csv 文件默认保存到“C:\Program Files (x86)\IIS Express”。为了准备将其部署到最终的休息/工作地点,我需要做些什么来保存文件,例如,保存到服务器的“Platypi”文件夹——有什么特别的吗?我是否必须专门设置某些文件夹柿子以允许写入“Platypi”。

是否只是更改此行的问题:

string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

...为此:

string csvFilename = string.Format(@"\Platypi\Platypus{0}.csv", dbContextAsInt);

?

最佳答案

对于 IIS 文件夹,应用程序有权在其中写入。我建议将文件写入 App_Data 文件夹。

当您想将文件保存在 IIS 应用程序文件夹之外时,您必须为 IIS 应用程序池的服务帐户(我认为默认情况下是 NETWORKSERVICE)提供对该文件夹的适当权限。

根据 B. Clay Shannon 的要求,我的实现:

string fullSavePath = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/Platypus{0}.csv", dbContextAsInt));

关于c# - 我可以从服务器上运行的 Web API 应用程序将文件写入服务器计算机上的文件夹吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639441/

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