Windows Develop Bookmark and Share   
 index > Windows Forms General > C# Problem foreach
 

C# Problem foreach

hello, can some take a look at this, it doesnt work as it should be


string[] fileid = { "1.txt", "2.txt", "3.txt"};
foreach (string fileidb in fileid)
{
string[] path = Directory.GetFiles(@"temp", fileidb, SearchOption.AllDirectories);
try
{
if (File.Exists(path[0]))
{
foreach (string fileidc in fileid)
{
Console.WriteLine("{0} ", fileidc);
}
}
}
catch (Exception)
{
}


the console output should be

1.txt
2.txt
3.txt



but it is this

1.txt
2.txt
3.txt
1.txt
2.txt
3.txt
1.txt
2.txt
3.txt

Power-Mosfet  Saturday, July 19, 2008 11:19 AM

Instead of:

if (File.Exists(path[0]))
{
foreach (string fileidc in fileid)
{
Console.WriteLine("{0} ", fileidc);
}
}

Do you mean:

if (File.Exists(path[0]))
{
Console.WriteLine("{0} ", fileidb);
}

Also, you can probably omit the File.Exists check. If Directory.GetFiles found it, the file exists (unless it got deleted after Directory.GetFiles found it).

BinaryCoder  Saturday, July 19, 2008 12:17 PM

Instead of:

if (File.Exists(path[0]))
{
foreach (string fileidc in fileid)
{
Console.WriteLine("{0} ", fileidc);
}
}

Do you mean:

if (File.Exists(path[0]))
{
Console.WriteLine("{0} ", fileidb);
}

Also, you can probably omit the File.Exists check. If Directory.GetFiles found it, the file exists (unless it got deleted after Directory.GetFiles found it).

BinaryCoder  Saturday, July 19, 2008 12:17 PM

You can use google to search for other answers

Custom Search

More Threads

• Vertical scrollbars on multi-column listbox.
• WinForm/Textbox - Multiple languages on the same form
• base64 string to image issues
• Composite User Controls - Linking event handlers from child controls
• Shortcut Assignment Not Diplaying On Button Controls
• Global hotkeys
• MessageBox problem with radio buttons
• How to make one property update another in a propetygrid
• drawing lines between controls
• 2 related combo boxes