【发布时间】:2015-03-07 15:49:25
【问题描述】:
代码如下:
enter code here
private void button1_Click(object sender, EventArgs e)
{
try
{
string[] Folder1 = Directory.GetFiles(txtFolder1.Text, comboBox1.SelectedItem.ToString());
string[] Folder1FileNames = new string[Folder1.Length];
int c = 0;
string fname1;
foreach (string f in Folder1)
{
fname1 = Path.GetFileName(f);
Folder1FileNames[c] = fname1;
c++;
}
string fname2;
string[] Folder2 = Directory.GetFiles(txtFolder2.Text, comboBox1.SelectedItem.ToString());
string[] Folder2FileNames = new string[Folder2.Length];
int t = 0;
foreach (string f in Folder2)
{
fname2 = Path.GetFileName(f);
Folder2FileNames[t] = fname2;
t++;
}
int m=0;
foreach (string f in Folder1FileNames)
{
while (f != Folder2FileNames[m] && m < Folder2FileNames.Length)
{
m++;
if (m == Folder2FileNames.Length)
{
Label newlabe = new Label();
newlabe.Text = f;
if(!listBox1.Items.Contains(newlabe.Text))
{
listBox1.Items.Add(newlabe.Text);
}
}
}
m = 0;
}
}
catch (Exception ex)
{
label1.Text = ex.Message;
}
我创建了两个数组,其中包含来自 SourceDirectory(Folder1) 和 CompareDirectory(Folder2) 的文件,然后创建了更多 2 个数组,其中包含没有完整 ptah 的文件名(以便在 araays 之间进行比较),然后我尝试比较数组,并将丢失的文件添加到列表框中。 问题是,当我尝试它时,它给了我源文件夹中第一个丢失的文件,(例如源文件夹包含下一个文件:a.txt、b.txt、c.txt、d.txt 和比较文件夹包含:a .txt,b.txt ,列表框中的结果会是 c.txt ,标签会显示:"you are out of the array index" ) 我该怎么办 ? 谢谢!
【问题讨论】:
标签: c# winforms directory compare