简体   繁体   中英

Reading content of sundry text files in a WPF C# application

EDIT: I am trying to read the content of sundry (in this case: 7) text files in a WPF C# application. Therefor, I use the following code, which has been edited:

try
        {
            List<string> Verknüpfung = new List<string>();

            foreach (string Datei in Directory.GetFiles(V.PfadShortcuts, "*.txt"))
            {
                Verknüpfung.AddRange(File.ReadAllLines(Datei, Encoding.UTF8));
            }

            for (int i = 0; i <= Verknüpfung.Count - 1; i += 2)
            {
                Image Icon = new Image();
                Icon.Source = new BitmapImage(new Uri(@"Images\Fugue Icons\document.png", UriKind.Relative));
                Icon.Height = 16;
                Icon.Width = 16;
                Icon.Stretch = Stretch.None;

                MenuItem MenuItem = new MenuItem();
                MenuItem.Click += delegate { Process.Start(Verknüpfung[1 + i]); };
                MenuItem.Header = Verknüpfung[0 + i];
                MenuItem.Icon = Icon;
                MenuItem.Padding = new Thickness(5);

                MI_Verknüpfungen.Items.Add(MenuItem);
            }
        }

I have 7 text files. Each contains the following: LINE 1: Title (like "Google"; for the HEADER) & LINE 2: Process (like " https://www.google.de/ "; for the CLICK EVENT).

However, I get this result (Sorry for hiding information, but it contains business matters): 错误信息

So it seems to work. But unfortunately, when I click on one of them, I get the following error message:

German: "Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein. Parametername: index"

English: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

It works, when I change the following:

Before: MenuItem.Click += delegate { Process.Start(Verknüpfung[1 + i]); }; MenuItem.Click += delegate { Process.Start(Verknüpfung[1 + i]); };

After: MenuItem.Click += new RoutedEventHandler(MenuItem_Click);

public void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i <= Verknüpfung.Count - 1; i += 2)
        {
            Process.Start(Verknüpfung[1 + i]);
        }
    }

But now, when I click on one of the menu items, the application runs ALL processes (meaning all 7 "shortcuts" saved in my collection) - however, the error message disappeared. How can I manage running only the process, that is clicked on?

Well let's analyse your problem... you said:

When I click on one of [the MenuItem s], I get the following error message:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

So you have a problem when you click on a MenuItem ... let's see what you declared should happen when a MenuItem is clicked:

MenuItem.Click += delegate { Process.Start(Verknüpfung[0 + i]); };

Bingo! There's your index parameter and you're passing in the value 0 + i . I'm curious though... why 0 + i , when that is the same as just i ? But anyway, so what does i equal?:

foreach (string Datei in Directory.GetFiles(V.PfadShortcuts, "*.txt"))
{
    ...
    i += 2;
}

What's that? i += 2 But that means that for every file, you're trying to access a position in the Verknüpfung collection that is twice the number of files that you have. Now you didn't show how you declared that collection, but if it's an array of some kind, then I'd guess that the i += 2 might work better if it were i++ instead.

Of course if your files have just two lines in them, then that would explain that, but that's the best that I can guess from what you've shown. If that is incorrect, then just put a breakpoint in the Click handler and check the value of i and the number of items in the Verknüpfung collection and then you'll know what the problem is.

Problem fixed! ;) The solution is to create a new var (tmp).

for (int i = 0; i <= Verknüpfung.Count - 1; i += 2)
            {
                Image Icon = new Image();
                Icon.Source = new BitmapImage(new Uri(@"Images\Fugue Icons\document.png", UriKind.Relative));
                Icon.Height = 16;
                Icon.Width = 16;
                Icon.Stretch = Stretch.None;

                var tmp = i;

                MenuItem MenuItem = new MenuItem();
                MenuItem.Click += delegate { Process.Start(Verknüpfung[1 + tmp]); };
                MenuItem.Header = Verknüpfung[0 + i];
                MenuItem.Icon = Icon;
                MenuItem.Padding = new Thickness(5);

                MI_Verknüpfungen.Items.Add(MenuItem);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM