Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
2.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.<br />
Parameter name: startIndex<br />

Code Part-//It's a files moving application...
C#
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);
      }

C#
static FileInfo[] ListDirectory(string path)
       {
           return (new DirectoryInfo(path)).GetFiles("*.*", System.IO.SearchOption.AllDirectories);
       }

C#
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);
}
C#
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
Updated 24-Jun-12 21:24pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Jun-12 3:13am    
In what line of code?
--SA

1 solution

If you used the debugger, you would figure it out in no time. You should always use it if you face any problems in your run-time behavior you cannot immediately fix, and you should certainly use it before asking questions like that. In it's not nice to ask such a question about an exception without comprehensive exception information, especially without marking the line(s) of code relevant to this exception.

From the code shown, one of the calls to System.String.Substring methods could throw this exception:
http://msdn.microsoft.com/en-us/library/system.string.substring.aspx[^].

The exception message is self-explanatory; you should check up actual string length and startIndex and compare.

—SA
 
Share this answer
 

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