2017-05-31 1038 views
0

我一直在嘗試上傳和導入CSV數據到我的數據表,但它一直出現一個錯誤。它聲明目錄名稱是無效的。在mscorlib.dll中發生了類型'System.IO.IOException'的異常,但未在用戶代碼中處理。目錄名無效

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** 
+2

是什麼csvPath' –

+1

看起來你正在使用的_FILE_的路徑來獲得在_directory_所有文件'的值。這是行不通的。在新的DirectoryInfo中使用'〜/ Files1 /'的路徑 –

+0

出現了另一個錯誤。附加信息:找不到路徑的一部分'C:\ Program Files(x86)\ IIS Express \ 〜\文件下載1' 。 – user8090359

回答

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

csvPath是文件路徑不是目錄路徑。

確保項目根目錄中有一個名爲「File1」的文件夾。

和..

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"); 
+0

哇感謝^^但是另一個錯誤出現在另一個代碼中。 string ConStr = ConfigurationManager.ConnectionStrings [「ConStr」]。ConnectionString;它表示對象引用未設置爲對象的實例。 – user8090359

+0

檢查此... https://www.aspsnippets.com/Articles/Read-Get-Connection-String-from-WebConfig-file-in-ASPNet-using-C-and-VBNet.aspx – levent

相關問題