0

我正在使用此代码在应用程序目录中查找 excel 文件的名称。但是,如果找不到该文件,我想给出一条错误消息。我怎么能用c#做到这一点?下面是我用来搜索文件的代码。

string linksfile;
string [] excelfile = Directory.GetFiles(Application.StartupPath + @"\", "*.xlsx");
linksfile = excelfile[0];

MessageBox.Show(linksfile);
4

2 回答 2

2

你应该检查一下if (excelFile.Length > 0)

于 2012-06-03T12:28:53.843 回答
1
    string linksfile;
    string [] excelfile = Directory.GetFiles(Application.StartupPath + @"\", "*.xlsx");

    if(excelfile.Length > 0)
    {
        linksfile = excelfile[0];

        MessageBox.Show(linksfile);
    }
    else
    {
         MessageBox.Show("File not found");
    }
于 2012-06-03T12:28:49.607 回答