I am getting an error , the error i am getting is Index was outside the bounds of the array.
My code is,
try
{
开发者_如何学Cstring path = (string)(Application.StartupPath + "\\TEMP\\TEMP_BACKFILL_atoz" + "\\" + name_atoz);
string reader1 = System.IO.File.ReadAllText(path);
string str = reader1;
//reader1.Close();
//reader1.Dispose();
File.Delete(path);
string[] Strarray = str.Split(new char[] { Strings.ChrW(10) });
int abc = Strarray.Length - 2;
int xyz = 0;
bool status = true;
string[] strarray1 = Strarray[xyz].Split(",".ToCharArray());//the line number 3696
string SecName = strarray1[0];
string SecSym = strarray1[1];
int DT = int.Parse(strarray1[2]);
int TM = int.Parse(strarray1[3]);
float O = float.Parse(strarray1[4]);
float H = float.Parse(strarray1[5]);
float L = float.Parse(strarray1[6]);
float C = float.Parse(strarray1[7]);
double V = double.Parse(strarray1[8]);
double OI = double.Parse(strarray1[9]);
}
catch (Exception)
{
}
the StackTrace say that the at Downloader.Form1.123_DoWork(Object sender, DoWorkEventArgs e) in C:\Documents and Settings\Administrator\Desktop\New Folder\Downloader\Downloader\Downloader\Downloader\Form1.cs:line 3696
Thanks In Advance
string[] strarray1 = Strarray[xyz].Split(",".ToCharArray());//the line number 3696
since this line is throwing the error, and xyz = 0, we must assume that strarray1 is empty at this point.
As a general rule, unless you are 100% certain about what your array contains, you should always check it's length before trying to access an element.
First point: Catching just the base class of the Exception object is never a very good idea, especially for debugging.
Sometimes its better to let visual studio break at a error-point and then to examine the variables. You could then check the length of Strarray, which I assume will be 0.
Second point: You have a lot of useless variables, especially int xyz = 0;
and bool status = true;
I would also like to mension that its very helpful to name the variables so that you can later tell what they are for. Names like "O" and "xyz" are totally useless and not helpful.
Now to your excact problem, System.IO.File.ReadAllLines(path)
returns an array of strings, one item per line. You should change your
string reader1 = System.IO.File.ReadAllText(path);
to string[] reader1 = System.IO.File.ReadAllLines(path);
then you can change
string[] strarray1 = Strarray[xyz].Split(",".ToCharArray());
to
string[] strarray1 = reader1[0].Split(",".ToCharArray());
That should solve your problem, but you should learn for further work with C# to name your variables correctly and to keep your code clean. After 1 month you won't know what "a" and "xyz" were for, blieve me :-)
精彩评论