计算后第三个文件夹中的文件副本g前两个文件夹之间的差异 [英] File copy in third folder after calculationg diff between first two folders

查看:69
本文介绍了计算后第三个文件夹中的文件副本g前两个文件夹之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在应用程序代码部分中给出以下错误的解决方案:
startIndex不能大于字符串的长度.
参数名称:startIndex


代码部分-//这是一个文件移动应用程序...
收合|复制代码
受保护的静态void copyDirectory(字符串Src,字符串Dst)
{
字符串targetPath = Dst.Substring(0,Dst.LastIndexOf(@"\")+1);
如果(!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
File.Copy(Src,Dst,true);
}
收合|复制代码
静态FileInfo [] ListDirectory(字符串路径)
{
return(new DirectoryInfo(path)).GetFiles("*.*",System.IO.SearchOption.AllDirectories);
}
收合|复制代码
private void button1_Browse(对象发送者,EventArgs e)
{
DialogResult结果= folderBrowserDialog1.ShowDialog();

if(result == DialogResult.OK)//测试结果.
{
textBox1.Text = folderBrowserDialog1.SelectedPath;

}
}
试试
{


FileInfo [] fileList1 = ListDirectory(dir1);
FileInfo [] fileList2 = ListDirectory(dir2);

var files1 =(从fileList1中的f1选择f1.FullName);

var filediff =(从文件1中的f1中选择f1.Substring(dir1.Length)).相交(从文件2中的f2中选择f2.Substring(dir1.Length));
foreach(filediff中的字符串s)
{
copyDirectory(dir2 + s,name2 + s);
}
收合|复制代码
捕获(IOException ex)
{
errlbl.Text ="*" + ex.ToString();
}
}
我已经接受了断点的帮助,但是现在我取消了对它的下线工作,因此无法解决它.请解决错误

can anyone give the solution of following error in Application Code Part:
startIndex cannot be larger than length of string.
Parameter name: startIndex


Code Part-//It''s a files moving application...
Collapse | Copy Code
protected static void copyDirectory(string Src, string Dst)
{
string targetPath = Dst.Substring(0, Dst.LastIndexOf(@"\") + 1);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
File.Copy(Src, Dst, true);
}
Collapse | Copy Code
static FileInfo[] ListDirectory(string path)
{
return (new DirectoryInfo(path)).GetFiles("*.*", System.IO.SearchOption.AllDirectories);
}
Collapse | Copy Code
private void button1_Browse(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();

if (result == DialogResult.OK) // Test result.
{
textBox1.Text = folderBrowserDialog1.SelectedPath;

}
}
try
{


FileInfo[] fileList1 = ListDirectory(dir1);
FileInfo[] fileList2 = ListDirectory(dir2);

var files1 = (from f1 in fileList1 select f1.FullName);

var filediff = (from f1 in files1 select f1.Substring(dir1.Length)).Intersect(from f2 in files2 select f2.Substring(dir1.Length));
foreach (String s in filediff)
{
copyDirectory(dir2 + s, name2 + s);
}
Collapse | Copy Code
catch (IOException ex)
{
errlbl.Text = "*" + ex.ToString();
}
}
I''ve already taken the help of breakpoint but I am unable to solve it now I undwerlined it. plz resolve th error

推荐答案

不要在上面的代码中使用子字符串,这不是使用
获取文件名的最佳方法
Path.GetFileName(f1)

可以解决您的问题
don''t use substring in the above code, that''s not the best way to get file name use

Path.GetFileName(f1)

which resolves your problem


这篇关于计算后第三个文件夹中的文件副本g前两个文件夹之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆