简体   繁体   中英

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll The directory name is invalid

I am trying to import a CSV file into a SQL server database. But when I select the file it breaks at var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ; with an error message:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll The directory name is invalid.

(note the "The directory name is invalid." has been translated from the dutch sentence "De mapnaam is ongeldig.")

     string server = "LOCALHOST";
    string database = "klantbestand";
    string SQLServerConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=true", server, database);


    string CSVpath = txtfilePath.Text; // CSV file Path
    string CSVFileConnectionString = String.Format(CSVpath);

    var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ;
    string File_Name = string.Empty;

    foreach (var file in AllFiles)
    {
        try
        {
            DataTable dt = new DataTable();
            using (OleDbConnection con = new OleDbConnection(CSVFileConnectionString))
            {
                con.Open();
                var csvQuery = string.Format("select * from [{0}]", file.Name);
                using (OleDbDataAdapter da = new OleDbDataAdapter(csvQuery, con))
                {
                    da.Fill(dt);
                }
            }

            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLServerConnectionString))
            {
                bulkCopy.ColumnMappings.Add(0, "Clientnr");
                bulkCopy.ColumnMappings.Add(1, "contact");
                bulkCopy.ColumnMappings.Add(2, "company");
                bulkCopy.ColumnMappings.Add(3, "address");
                bulkCopy.ColumnMappings.Add(4, "zipcode");
                bulkCopy.ColumnMappings.Add(5, "phone");
                bulkCopy.ColumnMappings.Add(6, "mobile");
                bulkCopy.ColumnMappings.Add(7, "email");
                bulkCopy.ColumnMappings.Add(8, "taxnumber");
                bulkCopy.ColumnMappings.Add(9, "BIC");
                bulkCopy.ColumnMappings.Add(10, "Bank");
                bulkCopy.ColumnMappings.Add(11, "SendMethod");
                bulkCopy.ColumnMappings.Add(12, "Active");
                bulkCopy.ColumnMappings.Add(13, "Notes");
                bulkCopy.ColumnMappings.Add(14, "PaymentMethod");
                bulkCopy.ColumnMappings.Add(15, "Mandate");
                bulkCopy.ColumnMappings.Add(16, "MandatDate");
                bulkCopy.ColumnMappings.Add(17, "CollectionType");
                bulkCopy.ColumnMappings.Add(18, "Country");
                bulkCopy.ColumnMappings.Add(19, "EmailIntro");
                bulkCopy.ColumnMappings.Add(20, "PaymentPeriod");
                bulkCopy.ColumnMappings.Add(21, "Reference");

                bulkCopy.DestinationTableName = "GegevensCSV";
                bulkCopy.BatchSize = 0;
                bulkCopy.WriteToServer(dt);
                bulkCopy.Close();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}

private void btnbrowse_Click(object sender, EventArgs e)
{
    OpenFileDialog filedialog = new OpenFileDialog();
    filedialog.Title = "kies csv file";
    filedialog.Filter = "csv Files|*.csv";
    filedialog.InitialDirectory = @"c:\";

    if(filedialog.ShowDialog()== DialogResult.OK)
    {
        txtfilePath.Text = filedialog.FileName;
    }

}

I am pretty sure the exception is thrown at this line

var AllFiles = new DirectoryInfo(CSVpath).GetFiles() ;

Its always a good thing to wrap this around a try catch statement. But as the exception message says either the paths is incorrect, so it doesnt exist or the syntax is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM