2012-04-21 20 views
0

我的下面的程序是一個簡單的窗體,用於搜索目錄中的文件,然後打開,讀取和寫入文件,之後有一個搜索按鈕,該文件,但我只能做到這一點與擴展名爲.txt的文件可以一些幫助我我也想這樣做的文檔,以及如果該文件是另一個擴展我想打開文件.txt和.doc擴展名我要彈出一個錯誤,它無法打開該文件,這是我下面的代碼,是有任何人誰可以幫我修改這個程序或者給我出出主意使用擴展名爲.txt和.doc的文件

namespace my_project 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog of = new OpenFileDialog(); 
      of.ShowDialog(); 
      textBox1.Text = of.FileName; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      StreamReader sr = new StreamReader(textBox1.Text); 
      richTextBox1.Text = sr.ReadToEnd(); 
      sr.Close(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      StreamWriter sw = new StreamWriter(textBox1.Text, true); 
      sw.WriteLine(textBox2.Text); 
      sw.Close(); 

     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      int index = 0; string temp = richTextBox1.Text; richTextBox1.Text = ""; richTextBox1.Text = temp; 
      while (index < richTextBox1.Text.LastIndexOf(textBox3.Text)) 
      { 
       richTextBox1.Find(textBox3.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None); 
       richTextBox1.SelectionBackColor = Color.Yellow; 
       index = richTextBox1.Text.IndexOf(textBox3.Text, index) + index; 
      } 
     } 
    } 
} 

回答

2

在.doc文件搜索會有點硬,因爲doc文件包含標記以便給你t的能力o裝飾你的文字(不同的字體,粗體,斜體,邊距等)。有第三方的圖書館和產品可以幫助你這個。另一方面,Txt文件是純文本文件,這就是爲什麼你沒有這個問題。

爲了實現驗證,您可以使用File靜態類並檢查文件的擴展名並決定下一步該做什麼。您也可以使用System.IO.Path.GetExtension方法,該方法接受文件名併爲您提供擴展名。

0

我不確定你在找什麼。這裏有幾個建議:

1)要顯示在您打開文件對話框只有.txt文件:

參考:OpenFileDialog Filter property

// Create an instance of the open file dialog box. 
OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

// Set filter options and filter index. 
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*"; 
openFileDialog1.FilterIndex = 1; 
... 

2)要查看某個文件的擴展名爲.txt,使用String.EndsWith():

參考:String.EndsWith() method

if (myfile.EndsWith (".txt", true, null) { 
    .. 

3)要調用默認程序.txt文件類型,使用ShellExec():

參考:UseShellExecute property

System.Diagnostics.ProcessStartInfo info = 
    new System.Diagnostics.ProcessStartInfo("c:\\temp\\myfile.txt"); 

    info.UseShellExecute = true; 
    info.Verb = "open"; 

    System.Diagnostics.Process.Start(info); 

「希望幫助!

2

爲了尋找Word文件,你需要這樣的代碼:

第一參考微軟12或14對象庫。

Microsoft.Office.Interop.Word.ApplicationClass wordObject = new ApplicationClass(); 
object file = textBox1.Text; //this is the path 
object nullobject = System.Reflection.Missing.Value; 
Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open 
(ref file, ref nullobject, ref nullobject, ref nullobject, 
ref nullobject, ref nullobject, ref nullobject, ref nullobject, 
ref nullobject, ref nullobject, ref nullobject, ref nullobject, 
ref nullobject, ref nullobject, ref nullobject, ref nullobject); 
docs.ActiveWindow.Selection.WholeStory(); 
docs.ActiveWindow.Selection.Copy(); 
IDataObject data = Clipboard.GetDataObject(); 
richTextBox1.Text = data.GetData(DataFormats.Text).ToString(); 
docs.Close(ref nullobject, ref nullobject, ref nullobject); 

如果你的目標.NET 4.0,它支持可選的參數,所以你不需要所有的nullobject的