2012-04-21 50 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的