0

I am trying to check if a CSV file exists and then save it to a MySQL table

Here is the code I have

    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);
                    }
                }
            }
        }
    }

It's this line that give me the error

foreach (var fi in diFileCheck.GetFiles())

The message says

"The directory name is invalid.\r\n"

I've just noticed my diFileCheck gives an error when I step into it: enter image description here

Joe
  • 69
  • 8

2 Answers2

0

Ok here's what I've done and seems to act as I want it to

    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);
            }
        }
    }

Thanks for all the tips!

Joe
  • 69
  • 8
-1

Check to see if the directory exists first using DirectoryInfo.Exists.

DirectoryInfo diFileCheck = new DirectoryInfo(strDirectory);
if(!diFileCheck.Exists)
     // do something here, possibly return
foreach (var fi in diFileCheck.GetFiles())
{
    // and so on
Joelius
  • 3,839
  • 1
  • 16
  • 36
Slothario
  • 2,830
  • 3
  • 31
  • 47
  • 1
    This gives a non-invocable member 'DirectoryInfo.Exists cannot be used like a method – Joe Dec 04 '19 at 17:41
  • 1
    It's not a method but a property, therefore you have to use `!diFileCheck.Exists`. I have submitted an edit to correct it. – Joelius Dec 04 '19 at 17:53
  • I note that this "fix" introduces several defects. First, it has a TOCTOU (time of check is not time of use) defect; if one process is deleting the dictionary while another is attempting to read it, the "exists" check can happen before the deletion and the GetFiles can happen after, and fail. That's rare. But there are other defects as well. Suppose the directory permissions allow the directory to be checked for existence, but not read, for example? This is the wrong solution. The correct solution is to try-catch around all operations that can fail, and handle the failures. – Eric Lippert Dec 04 '19 at 22:28