2014-01-12 36 views
2

我從一個目錄本地機器上的一個按鈕單擊事件打開Word文件中的代碼:顯示從目錄列表框中的Word文件列表(Asp.net C#)

`string path = @"C:\Users\Ansar\Documents\Visual Studio 2010\Projects\GoodLifeTemplate\GoodLifeTemplate\Reports\IT"; 
    List<string> AllFiles = new List<string>(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ParsePath(); 
     } 
    } 
    void ParsePath() 
    { 
     string[] SubDirs = Directory.GetDirectories(path); 
     AllFiles.AddRange(SubDirs); 
     AllFiles.AddRange(Directory.GetFiles(path)); 
     int i = 0; 
     foreach (string subdir in SubDirs) 
     { 
      ListBox1.Items.Add(SubDirs[i].Substring(path.Length + 1, subdir.Length - path.Length - 1).ToString()); 

      i++; 
     } 

     DirectoryInfo d = new DirectoryInfo(path); 
     FileInfo[] Files = d.GetFiles("*.doc") 
     ListBox1.Items.Clear(); 
     foreach (FileInfo file in Files) 
     { 
      ListBox1.Items.Add(file.ToString()); 
     } 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (ListBox1.SelectedItem != null) 
     { 
      Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application(); 
      Document document = ap.Documents.Open(path + "\\" + ListBox1.SelectedItem.Text); 
     } 
     else 
     { 
      ScriptManager.RegisterStartupScript(this, GetType(), "error", "Please select file;", true); 
     } 

`

這是顯示列表框中的所有單詞文件列表,因爲我的要求是,但也是以前打開的臨時文件是(~$nameoffile.docx)我不想在列表框中顯示此(~$nameoffile.docx)

回答

1

文件~$[something].docx是隱藏文件。我會建議你做的是確保你篩選出來,如:

System.IO.DirectoryInfo dirInf = new System.IO.DirectoryInfo(@"C:\myDir\Documents"); 
var files = dirInf.GetFiles("*.doc").Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden).ToArray(); 

dirInf.GetFiles作品以同樣的方式在Windows所做的搜索模式。

這是一個Lambda Expression

.Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden) 

這是一個bitwise comparison

(f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden 
+0

非常感謝它按我的工作需求量的感謝 – 3333

+0

不少,其良好的 – 3333