C# 如何将URI路径文件复制到临时文件夹?
我在URI中有一个文件路径,并试图将URI文件复制到C:\temp\中,但如果有人建议我,我会得到一个错误“找不到文件(从URI路径)”。多谢各位C# 如何将URI路径文件复制到临时文件夹?,c#,file,copy,uri,C#,File,Copy,Uri,我在URI中有一个文件路径,并试图将URI文件复制到C:\temp\中,但如果有人建议我,我会得到一个错误“找不到文件(从URI路径)”。多谢各位 public String getFile(String uri) { // Download file in temp folder String fileName = Path.GetFileName(uri.Replace("/", "\\")); Uri fileUri = new
public String getFile(String uri)
{
// Download file in temp folder
String fileName = Path.GetFileName(uri.Replace("/", "\\"));
Uri fileUri = new Uri(uri);
string fullFilePath = absoluteUri.AbsoluteUri.ToString();
string localPath = new Uri(fullFilePath).LocalPath;
String tempFolder = @"C:\temp\";
File.Copy(localPath, tempFolder);
return fileName;
}
您可以使用web客户端从Uri下载文件
new WebClient().DownloadFile(uri, Path.Combine(filePath, fileName));
我尝试了另一种方式,它对我很有效。我希望这能对其他人有所帮助
private String getFile(String uri)
{
Uri uriFile = new Uri(uri);
String fileName = Path.GetFileName(uri);
List<String> fileData = new List<String>();
// Reads all the code lines of a file
fileData = readCodeLines(uriFile);
String tempPath = @"c:\temp\";
try
{
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
File.WriteAllLines(tempPath, fileData);
}
catch (IOException ex)
{
MessageBox.Show("Could not find the Temp folder" + " " + tempPath);
}
return fileName;
}
私有字符串getFile(字符串uri)
{
Uri uriFile=新的Uri(Uri);
字符串文件名=Path.GetFileName(uri);
List fileData=新列表();
//读取文件的所有代码行
fileData=readCodeLines(uriFile);
字符串tempPath=@“c:\temp\”;
尝试
{
如果(!Directory.Exists(tempPath))
{
CreateDirectory(tempPath);
}
writeAllines(临时路径、文件数据);
}
捕获(IOEX异常)
{
MessageBox.Show(“找不到临时文件夹”+“”+tempPath);
}
返回文件名;
}
谢谢您的建议。但我得到一个错误:在WebClient请求期间发生异常。@Sunny实际异常或内部异常是什么?您传递的Uri是什么?我传递的是SVN文件路径。内部异常:System.ArgumentException:不支持Uri格式。\r\n位于System.IO.path.NormalizePath(字符串路径、布尔fullCheck、Int32 maxPathLength、布尔ExpandShortPath)\r\n位于System.IO.path.NormalizePath(字符串路径、布尔fullCheck、Int32 maxPathLength)\r\n位于System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔检查主机)\r\n位于System.IO.FileStream(字符串路径、文件模式、文件访问权限)\r\n在System.Net.WebClient.DownloadFile(Uri地址、字符串文件名)“字符串