while...循环在 public 静态方法 C# 中的 if 语句之后结束

while... cycle ends after if statement in public static method C#

我在 public 带有循环 "while" 的静态方法中写了一些代码。但是这个循环在 "if" 语句之后结束,应用程序不会抛出任何异常。这是代码:

public static void ShortcutDetect()
{
    ShortkeyIndex = 0;
    while(ShortkeyIndex < 1000) 
    {
        File.WriteAllText(@"C:\Users\OEM\Desktop\log.txt",
            File.ReadAllText(@"C:\Users\OEM\Desktop\log.txt") + Convert.ToString(ShortkeyIndex));
        if(Program.key.Replace("LShiftKey","Shift")
            .Replace("RShiftKey","Shift").Replace("RMenu","Alt")
            .Replace("LMenu","Alt").Replace("RControlKey","Ctrl")
            .Replace("LControlKey","Ctrl").EndsWith(RawShortkeys[ShortkeyIndex]))
        {
            MessageBox.Show(RawShortkeys[ShortkeyIndex]);
        }
        ShortkeyIndex++;
    }
}

先谢谢了。

让我们正确地实施它:

public static void ShortcutDetect() {
  // Take loop independent code out of the loop:
  // and, please, format it out:
  var source = Program.key
    .Replace("LShiftKey", "Shift")
    .Replace("RShiftKey", "Shift")
    .Replace("RMenu", "Alt")
    .Replace("LMenu", "Alt")
    .Replace("RControlKey", "Ctrl")
    .Replace("LControlKey", "Ctrl");

  // Wrong type of loop (while): what is ShortkeyIndex?
  // where has it been declared, why 1000?
  // Please, have a look how the right loop easy to implement and read   
  foreach (var item in RawShortkeys) {
    // Debug: let's output item onto Console
    // Console.WriteLine(item);
    // Debug: ...or in the file
    // File.AppendAllText()@"C:\Users\OEM\Desktop\log.txt", " " + item); 

    if (source.EndsWith(item)) // <- put a break point here, inspect item's
      MessageBox.Show(item);
  }
}