Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Microsoft Azure虚拟服务器上创建和保存临时文件_C#_Azure - Fatal编程技术网

C# 如何在Microsoft Azure虚拟服务器上创建和保存临时文件

C# 如何在Microsoft Azure虚拟服务器上创建和保存临时文件,c#,azure,C#,Azure,我正在为我的站点使用免费的MS Azure虚拟Web服务器 在我的开发机器上,我可以成功地创建一个CSV文件,将其保存到相对临时目录,然后将其下载到浏览器客户端 但是,当我从Azure站点运行它时,会出现以下错误: System.IO.DirectoryNotFoundException:找不到 路径“D:\home\site\wwwroot\temp\somefile.csv” Azure网站的免费版本是否阻止我们将文件保存到磁盘?如果没有,我们可以在哪里创建/保存动态生成的文件 代码示例 p

我正在为我的站点使用免费的MS Azure虚拟Web服务器

在我的开发机器上,我可以成功地创建一个CSV文件,将其保存到相对临时目录,然后将其下载到浏览器客户端

但是,当我从Azure站点运行它时,会出现以下错误:

System.IO.DirectoryNotFoundException:找不到 路径“D:\home\site\wwwroot\temp\somefile.csv”

Azure网站的免费版本是否阻止我们将文件保存到磁盘?如果没有,我们可以在哪里创建/保存动态生成的文件

代码示例

private FilePathResult SaveVolunteersToCsvFile(List<Volunteer> volunteers)
{
    string virtualPathToDirectory = "~/temp";
    string physicalPathToDirectory = Server.MapPath(virtualPathToDirectory);
    string fileName = "Volunteers.csv";
    string pathToFile = Path.Combine(physicalPathToDirectory, fileName);
    StringBuilder sb = new StringBuilder();

    // Column Headers
    sb.AppendLine("First Name,Last Name,Phone,Email,Approved,Has Background Check");

    // CSV Rows
    foreach (var volunteer in volunteers)
    {
        sb.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6}",
            volunteer.FirstName, volunteer.LastName, volunteer.MobilePhone.FormatPhoneNumber(), volunteer.EmailAddress, volunteer.IsApproved, volunteer.HasBackgroundCheckOnFile));
    }

    using (StreamWriter outfile = new StreamWriter(pathToFile))
    {
        outfile.Write(sb.ToString());
    }
    return File(Server.MapPath(virtualPathToDirectory + "/" + fileName), "text/csv", fileName);
}
private FilePathResult saveOrganizerStocsvFile(列出志愿者)
{
字符串virtualPathToDirectory=“~/temp”;
字符串physicalPathToDirectory=Server.MapPath(virtualPathToDirectory);
字符串fileName=“志愿者.csv”;
string pathToFile=Path.Combine(物理路径目录,文件名);
StringBuilder sb=新的StringBuilder();
//列标题
sb.AppendLine(“名字、姓氏、电话、电子邮件、批准、有背景调查”);
//CSV行
foreach(志愿者中的志愿者)
{
sb.AppendLine(string.Format(“{0},{1},{2},{3},{4},{5},{6}”),
志愿者.FirstName,志愿者.LastName,志愿者.MobilePhone.FormatPhoneNumber(),志愿者.EmailAddress,志愿者.IsApproved,志愿者.HasBackgroundCheckOnFile));
}
使用(StreamWriter outfile=新StreamWriter(pathToFile))
{
写出(某人写的东西);
}
返回文件(Server.MapPath(virtualPathToDirectory+“/”+文件名),“text/csv”,文件名);
}

Azure网站提供了环境变量,您可以使用这些变量来访问诸如临时存储文件夹之类的内容。例如,您可以访问一个“TEMP”变量,以获取特定于您的网站的TEMP文件夹的路径

将方法中的第2行更改为:

        //string physicalPathToDirectory = Server.MapPath(virtualPathToDirectory);
        string physicalPathToDirectory = Environment.GetEnvironmentVariable("TEMP");
然后将最后一行更改为:

        //return File(Server.MapPath(virtualPathToDirectory + "/" + fileName), "text/csv", fileName);
        return File(pathToFile, "text/csv", fileName);

确保将
~/temp
文件夹发布到服务器,因为您的发布过程可能没有包含它。

您确定路径正确吗?您是否尝试过使用类似于
Server.MapPath(“~/App\u Data/temp/somefile.csv”)?(你应该提供你用来保存文件的代码)@mason-Updated OP with code你确定服务器上存在“~/temp”文件夹吗?不!我刚刚在本地“/temp”文件夹上强制“发布”,现在一切都好了。谢谢,梅森。如果你把它作为你的答案,我会记下来的。另一方面,当我发布我的站点时,我的本地“/temp”文件夹没有被发布,这对我来说很奇怪。
System.IO.Path.GetTempFileName()
是否在Azure Wenbsite环境中不起作用?