如果和其他如果指示

D.I*_*ple 0 c# if-statement

我有一个带doc文件的目录(名字名字)

例如我有这个:

Doe John.doc

Doe Phil.doc

Doe Robert.doc

Poe Dameron.doc

我有一个包含数据库中所有人的列表框,我选择一个名称,然后单击"打开文件"

我的代码:

       string fullname = Name + " " + Firstname; 

       string[] allFiles = Directory.GetFiles((Doc_Path));
       foreach (string file in allFiles)
       {
           if (file.Contains(fullname))
           {
               Process.Start(file);
               return;
           }
       // if it cant found fullname, try to open by Name only
           else if(file.Contains(Name)) 
           {
               Process.Start(file);
               return;
           }
       }
Run Code Online (Sandbox Code Playgroud)

我的问题 :

如果我选择Doe Robert,它会在所有情况下打开Doe John,但它应该在第一个IF指令处停止

即使它是基础知识我也不明白:/

Pat*_*man 5

你实际上想先做一个完全匹配,然后进行模糊匹配.您必须再次走路foreach以便稍后检查您的模糊匹配:

foreach (string file in allFiles)
{
    if (file.Contains(fullname))
    {
        Process.Start(file);
        return;
    }
}

foreach (string file in allFiles)
{
    // if it cant found fullname, try to open by Name only
    if(file.Contains(Name)) 
    {
        Process.Start(file);
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

return代码的其余部分没有在第一场比赛中执行的原因找到.