C# 如何克服以下错误:System.IO.IOException:';目录名无效。在我的foreach循环中?

C# 如何克服以下错误:System.IO.IOException:';目录名无效。在我的foreach循环中?,c#,getfiles,C#,Getfiles,我试图检查CSV文件是否存在,然后将其保存到MySQL表中 这是我的密码 public void SaveCsvToDatabase(string strDirectory) { if (strDirectory.Length > 0) { if (ConnectToDatabase()) { //And specify the name of the file

我试图检查CSV文件是否存在,然后将其保存到MySQL表中

这是我的密码

    public void SaveCsvToDatabase(string strDirectory)
    {
        if (strDirectory.Length > 0)
        {
            if (ConnectToDatabase())
            {
                //And specify the name of the file in the path we want. 
                DirectoryInfo diFileCheck = new DirectoryInfo(strDirectory);
                foreach (var fi in diFileCheck.GetFiles())
                {
                    string strSourceFile = fi.FullName;
                    if (File.Exists(strSourceFile))
                    {
                        //save our file to the database
                        string strFileInsert = "INSERT INTO InvoiceHdr" +
                                               " (fileName) VALUES ('" + MySqlHelper.EscapeString(strSourceFile) + "')";

                        MySqlCommand command = new MySqlCommand(strFileInsert, con);
                        if (command.ExecuteNonQuery() == 1)
                        {
                            //we've inserted our row, now get the new id
                            m_iFileId = command.LastInsertedId;
                            if (m_iFileId != 0)
                            {
                                SaveCsvLines(strSourceFile);
                            }
                            ArchiveFile();
                        }
                    }
                    else
                    {
                        string strMessage = "No file found in directory: \n\n" + strDirectory;
                        MessageBox.Show(strMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
是这一行给了我错误

foreach (var fi in diFileCheck.GetFiles())
消息说

“目录名无效。\r\n”

我刚刚注意到,当我进入DefileCheck时,它会给出一个错误:

首先使用检查目录是否存在


好的,这就是我所做的,并且似乎按照我的意愿行事

    public void SaveCsvToDatabase(string strDirectory)
    {
        if (ConnectToDatabase())
        {
            //We need to specify file we are looking to process exists.
            int fileCount = Directory.GetFiles(@"\\company\Archive\IN\InvoiceTest\Inbox\").Length;
            //And specify the name of the file in the path we want. 
            DirectoryInfo diFileCheck = new DirectoryInfo(@"\\company\EDIArchive\IN\InvoiceTest\Inbox\");
            foreach (var file in diFileCheck.GetFiles())
            {
                string strSourceFile = file.FullName;
                if (File.Exists(strSourceFile))
                {
                    //save our file to the database
                    string strFileInsert = "INSERT INTO InvoiceHdr" +
                                           " (fileName) VALUES ('" + MySqlHelper.EscapeString(strSourceFile) + "')";

                    MySqlCommand command = new MySqlCommand(strFileInsert, con);
                    if (command.ExecuteNonQuery() == 1)
                    {
                        //we've inserted our row, now get the new id
                        m_iFileId = command.LastInsertedId;
                        if (m_iFileId != 0)
                        {
                            SaveCsvLines(strSourceFile);
                        }

                    ArchiveFile();

                    }
                }
            }
            if (fileCount == 0)
            {
                //If we cant count any files in the specified path, we can display a message box warning us. 
                string strMessage = "No file found in directory: \n\n" + strDirectory;
                MessageBox.Show(strMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

谢谢你的提示

您是否已仔细检查以确保存储在
strSourceFile
中的文件路径是有效路径?strDirectory的任何值是否都会发生这种情况?似乎是strDirectory的任何值都会发生这种情况?我在DefileCheck和strDirectory中获得有效路径
strDirectory
中的路径是什么?它是另一台计算机上的共享吗?@Joe使用预先准备好的语句来避免SQL注入,请参阅这提供了一个非invocable成员'DirectoryInfo.Exists不能像方法一样使用它不是方法而是属性,因此您必须使用
!DefileCheck.Exists
。我已经提交了一个编辑来更正它。我注意到这个“修复”引入了几个缺陷。首先,它有TOCTOU(检查时间不是使用时间)缺陷;如果一个进程正在删除词典,而另一个进程正在尝试读取词典,那么“存在”检查可能会在删除之前进行,而GetFiles可能会在删除之后进行,并且会失败。那很少见。但也存在其他缺陷。例如,假设目录权限允许检查目录是否存在,但不允许读取?这是错误的解决方案。正确的解决方案是尝试捕获所有可能失败的操作,并处理这些失败。
    public void SaveCsvToDatabase(string strDirectory)
    {
        if (ConnectToDatabase())
        {
            //We need to specify file we are looking to process exists.
            int fileCount = Directory.GetFiles(@"\\company\Archive\IN\InvoiceTest\Inbox\").Length;
            //And specify the name of the file in the path we want. 
            DirectoryInfo diFileCheck = new DirectoryInfo(@"\\company\EDIArchive\IN\InvoiceTest\Inbox\");
            foreach (var file in diFileCheck.GetFiles())
            {
                string strSourceFile = file.FullName;
                if (File.Exists(strSourceFile))
                {
                    //save our file to the database
                    string strFileInsert = "INSERT INTO InvoiceHdr" +
                                           " (fileName) VALUES ('" + MySqlHelper.EscapeString(strSourceFile) + "')";

                    MySqlCommand command = new MySqlCommand(strFileInsert, con);
                    if (command.ExecuteNonQuery() == 1)
                    {
                        //we've inserted our row, now get the new id
                        m_iFileId = command.LastInsertedId;
                        if (m_iFileId != 0)
                        {
                            SaveCsvLines(strSourceFile);
                        }

                    ArchiveFile();

                    }
                }
            }
            if (fileCount == 0)
            {
                //If we cant count any files in the specified path, we can display a message box warning us. 
                string strMessage = "No file found in directory: \n\n" + strDirectory;
                MessageBox.Show(strMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }