file.copy()方法异常 [英] file.copy() method exception
本文介绍了file.copy()方法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我必须将图像文件从一个文件夹复制到另一个文件夹
hi all
i have to copy an image file from one folder to another folder
if (isPath.Contains(@"\"))
{
string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ImgPath"].ToString();
DirectoryInfo di = new DirectoryInfo(path+@"\"+isPath);
DirectoryInfo di1 = new DirectoryInfo(path+@"\Bilder\");
var directories = di.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo d in directories)
{
if (d.Extension == ".jpg")//Bilder
{
CreateFolder("Bilder");
// File.Open(d.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
File.OpenRead(d.FullName);
// File.OpenWrite(@"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\");
// File.Open(di1.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder");
//File.Copy(
}
else if (d.Extension == ".xml")//EFOEksport
{
CreateFolder("EFOEksport");
System.IO.File.Copy(d.FullName, d.Directory.Name, false);
}
else
{
//Nothing
}
}
会引发异常
找不到路径的一部分
plz help [已消除紧急性]
it throws an exception
could not find a part of path
plz help [Removed urgency]
推荐答案
File.Copy 需要绝对路径("C:\ Users \ Desktop \ testFolder").当给定File.Copy的路径是相对路径(/testfolder)时,它将引发异常找不到路径的一部分".
在您的情况下,只需确保给出的路径是绝对的即可.这将解决您的问题.
File.Copy require absolute path("C:\Users\Desktop\testFolder"). when the path given to File.Copy is relative(/testfolder), it will throw an exception "could not find a part of path".
In your case just make sure that the path given is absolute. This will fix your issue.
我看到了几个问题...
1)File.OpenRead返回为System.IO.FileStream,现在对该文件具有锁定,直到关闭流为止,您将收到一个有关文件正在被另一个进程使用的异常.我看不到您正在阅读文件,因此请忽略.
2)System.IO.File.Copy必须同时具有两个参数的文件名,您不能使用目标目录,还必须添加目标文件名
例如
I see a couple of issues...
1) File.OpenRead returns as System.IO.FileStream and now has a lock on that file until the stream is closed, you will receive an exception about the file is in use by another process. I do not see you reading the file so ommit that.
2) The System.IO.File.Copy needs to have a file name for both parameters, you cannot use a destination directory you must add the destination filename as well
e.g.
System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\" + d.Name;);
foreach (FileInfo d in directories)
{
if (d.Extension == ".jpg")//Bilder
{
CreateFolder("Bilder");
File.OpenRead(d.FullName);
//you may want to check if file exists at destination already and implement action based on that (overwrite or rename with numeric copy value file(Copy 1).ext etc. just a suggestion to avoid exceptions in future
System.IO.File.Copy(d.FullName, @"D:\Projects\EFO\Project_Synchronizer\Synchronizer WEB\otraWebService\otraWebService\Bilder\" + d.Name);
}
这篇关于file.copy()方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文