简体   繁体   中英

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code. The directory name is invalid

I've been trying to upload and import csv file into my datatable but it keeps coming out an error. It states that the directory name is invalid.

    protected void Upload(object sender, System.EventArgs e)
    {

        const string CSV_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"text;HDR=YES;FMT=Delimited\"";

        string csvPath = Server.MapPath("~/Files1/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(csvPath);


        **var AllFiles = new DirectoryInfo(csvPath).GetFiles("*csv"); - error here**
string csvPath = Server.MapPath("~/Files1/") + Path.GetFileName(FileUpload1.PostedFile.FileName);

csvPath is file path not directory path.

Make sure there is a folder named "File1" in the project root directory.

and..

    string csvPath = Server.MapPath("~/Files1/");
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(Path.Combine(csvPath,fileName));
    var AllFiles = new DirectoryInfo(csvPath).GetFiles("*csv");

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