求问,为什么用foreach后 数组的每一个元素都被覆盖了
求问,为什么用foreach后 数组的每一个元素都被覆盖了第一个是自己定义的一个类,第二个是调用类写入一个ReadFile【】数组中。但遍历后加入的元素会把数组前面的元素覆盖了,求解怎么解决这个问题?
{
public string ReadName;
public string FilePath;
public ReadFile()
{
ReadName="Name";
FilePath="Path";
}
public ReadFile(string _name,string _path)
{
for (int i = 0; _name != null; i++)
{
ReadName = _name;
FilePath = _path;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
string[] name = new string [512];
ReadFile[] FNames=new ReadFile[512];
ReadFile RF = new ReadFile();
int length =0;
TSTime.Text = System.DateTime.Now.ToString();
string path=() +@"\Mydiary\";
if(Directory.Exists(path)==false)
{
Directory.CreateDirectory(path);
}
foreach (string FileName in Directory.GetFiles(path))
{
string FName;
FName = FileName.Substring(path.Length,FileName.Length-path.Length-4);
RF.ReadName = FName;
RF.FilePath = FileName;
FNames.SetValue(RF,length);
length++;
}
treeView.Nodes.Clear();
AddNodes(FNames);
}