Click here to Skip to main content
16,017,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 19:49pm    
Please never re-post. You should use the page of your original question.
--SA

1 solution

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
 
Share this answer
 
Comments
ravindrasingh1 25-Jun-12 8:12am    
after replacing f1.Substring(dir1.Length)) with Path.GetFileName(f1)) I am getting error message ABC File Not Found.
ravindrasingh1 25-Jun-12 8:15am    
My code is working properly when I am working on console application but it's not working when I am doing with Winform.
[no name] 25-Jun-12 9:46am    
This is a file copy program it has nothing to do with Windows or console application, the issue then has something else....
ravindrasingh1 26-Jun-12 4:02am    
Path.GetFileName(f1) is giving error of file not found

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900