2009-10-31 23 views
0

我試圖以編程方式建立文件夾中的文件列表,具有文件大小和修改日期等特定屬性。asp.net System.IO.IOException

我可以返回文件名,但任何其他屬性都會引發錯誤:System.IO.IOException:文件名,目錄名或卷標語法不正確。

我在這裏錯過了什麼?

private void BuildDocList() 
    { 
     var files = Directory.GetFiles(Server.MapPath(FilePath)); 

     foreach (var f in files) 
     { 
      var file = new FileInfo(FilePath + f); 
      var fileItem = new ListItem(); 

      // this line works fine 
      fileItem.Text = file.Name.Split('.')[0] + ", "; 


      // this line causes the runtime error 
      fileItem.Text = file.CreationTime.ToShortDateString(); 

      FileList.Items.Add(fileItem); 
     } 

    } 

回答

1

您試圖對FileInfo使用錯誤的文件名 - 您正在使用未映射的路徑。你應該用這樣的東西:

string directory = Server.MapPath(FilePath); 
string[] files = Directory.GetFiles(directory); 

foreach (string f in files) 
{ 
    FileInfo file = new FileInfo(Path.Combine(directory, f)); 
    // Now the properties should work. 
+0

這樣做了,謝謝! =) – shimonyk 2009-10-31 14:53:15

相關問題