C# 使用WPF C将图像文件复制到中的空文件夹#
在检查了所有与复制文件有关的内容之后,我找不到我的问题的答案 尝试将文件复制到WPF应用程序中的空文件夹时出现异常问题。下面是代码片段C# 使用WPF C将图像文件复制到中的空文件夹#,c#,wpf,C#,Wpf,在检查了所有与复制文件有关的内容之后,我找不到我的问题的答案 尝试将文件复制到WPF应用程序中的空文件夹时出现异常问题。下面是代码片段 public static void Copy() { string _finalPath; foreach (var name in files) // name is the filename extracted using GetFileName in a list of strings { _finalPath =
public static void Copy()
{
string _finalPath;
foreach (var name in files) // name is the filename extracted using GetFileName in a list of strings
{
_finalPath = filePath; //it is the destination folder path e.g,C:\Users\Neha\Pictures\11-03-2014
if(System.IO.Directory.Exists(_finalPath))
{
_finalPath = System.IO.Path.Combine(_finalPath,name);
System.IO.File.Copy(name, _finalPath , true);
}
}
}
调试时,file.copy()语句发生异常,该语句表示
“FileNotFoundException未处理”找不到文件
我已经知道组合路径和复制的其他方面,但我不知道为什么会提出这个例外。
我是WPF的noob,请帮助………获取文件名()
仅返回文件的实际名称(删除路径),您需要的是文件的完整路径。因此,由于驱动器上不存在“名称”(路径未知)您是变量,
name
,很可能只是文件名(即something.jpg)。使用File.Copy(…)
方法时,如果不提供绝对路径,该方法将采用相对于可执行文件的路径
基本上,如果您在C:\Projects\SomeProject\bin\Debug\SomeProject.exe中运行应用程序,则假定您的文件是C:\Projects\SomeProject\bin\Debug\SomeProject.jpg。使用以下代码:
public static void Copy()
{
string _finalPath;
var files = System.IO.Directory.GetFiles(@"C:\"); // Here replace C:\ with your directory path.
foreach (var file in files)
{
var filename = file.Substring(file.LastIndexOf("\\") + 1); // Get the filename from absolute path
_finalPath = filePath; //it is the destination folder path e.g,C:\Users\Neha\Pictures\11-03-2014
if (System.IO.Directory.Exists(_finalPath))
{
_finalPath = System.IO.Path.Combine(_finalPath, filename);
System.IO.File.Copy(file, _finalPath, true);
}
}
}
如果System.IO.File.Copy引发FileNotFoundException,则可以确定源文件的路径不正确。在引发异常之前,请尝试放置断点文件.Copy(名称,…)。联合收割机是否有问题?您是否尝试过System.IO.File.Copy(名称,\u finalPath+“\”+名称,true)?您从哪里获得
文件
列表?您应该使用System.IO.File.Exists(name)
检查name
是否存在。尝试放置@symbol<代码>\u finalPath=@filePath代码>所以你想说我应该在源文件和目标文件夹的绝对路径上使用combine方法???不,我不能说解决方案是什么,因为没有足够的信息。问题是文件只包含文件名,而不是完整路径。无论您从何处获取文件
,它们都应作为FileInfo
对象或绝对路径字符串返回,除非您真正需要的是相对路径(事实并非如此).之前我是用文件的完整路径做的,但同样的问题也存在,然后我读到stackoverflow本身,你应该把文件名和文件夹路径结合起来。。。。。。