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: