C# 创建文件夹并保存在临时文件夹中

C# 创建文件夹并保存在临时文件夹中,c#,winforms,windows-8,visual-studio-2012,C#,Winforms,Windows 8,Visual Studio 2012,我有Visual Studio 11(Windows 8 Developer)我创建了一个下载文件: string sUrlToReadFileFrom = "http://mysite/1.mp3"; int iLastIndex = sUrlToReadFileFrom.LastIndexOf('/'); string sDownloadFileName = sUrlToReadFileFrom.Substring(iLastIndex + 1, (sUrlToReadFileFrom.Le

我有Visual Studio 11(Windows 8 Developer)我创建了一个下载文件:

string sUrlToReadFileFrom = "http://mysite/1.mp3";
int iLastIndex = sUrlToReadFileFrom.LastIndexOf('/');
string sDownloadFileName = sUrlToReadFileFrom.Substring(iLastIndex + 1, (sUrlToReadFileFrom.Length - iLastIndex - 1));
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri("http://mysite/1.mp3"), "C:\\Windows\\Temp" + "\\" + sDownloadFileName);
但它不能启动!如果我更改“E:\Temp”中的文件夹“C:\Windows\Temp”,下载将开始。驱动器C:\不工作,为什么?
可以保存在临时文件夹中,或者您有其他想法?

使用环境变量

Environment.GetFolderPath(Environment.LocalApplicationData)
试试这个:

string tempPath = System.IO.Path.GetTempPath();

工作正常吗?

您可以使用临时文件夹路径:

string tempPath = System.IO.Path.GetTempPath();

尚未使用Widnows 8,这只是猜测,但您可能没有对C:\上该位置的写入权限(作为标准特权用户)。

请使用以下选项之一:

  • 例如
    LocalApplicationData
    MyDocuments

+1尽早采用W8:)感谢您的回答!记住:Windows是最好的