Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用扩展名为.txt和.doc的文件_C# - Fatal编程技术网

C# 使用扩展名为.txt和.doc的文件

C# 使用扩展名为.txt和.doc的文件,c#,C#,我下面的程序是一个简单的windows窗体,它在目录中搜索一个文件,然后打开,在上面读写,然后有一个搜索按钮,搜索文件中的单词,但我只能对扩展名为.txt的文件执行此操作。有什么帮助吗?我也想对word文档执行此操作,我想打开扩展名为.txt和.doc的文件如果该文件是另一个扩展名,我想弹出一个错误,它无法打开该文件这是我下面的代码,有人可以帮我修改这个程序或给我一些想法吗 namespace my_project { public partial class Form1 : Form

我下面的程序是一个简单的windows窗体,它在目录中搜索一个文件,然后打开,在上面读写,然后有一个搜索按钮,搜索文件中的单词,但我只能对扩展名为.txt的文件执行此操作。有什么帮助吗?我也想对word文档执行此操作,我想打开扩展名为.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;
            }
        }
    }
}
名称空间my_项目
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
OpenFileDialog of=新建OpenFileDialog();
of.ShowDialog();
textBox1.Text=of.FileName;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
StreamReader sr=新的StreamReader(textBox1.Text);
richTextBox1.Text=sr.ReadToEnd();
高级关闭();
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
StreamWriter sw=新的StreamWriter(textBox1.Text,true);
sw.WriteLine(textBox2.Text);
sw.Close();
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
int index=0;字符串temp=richTextBox1.Text;richTextBox1.Text=“”;richTextBox1.Text=temp;
while(索引
在.doc文件中搜索会有点困难,因为doc文件包含标记,以便您能够装饰文本(使用不同的字体、粗体、斜体、边距等)。有第三方库和产品可能会帮助您实现这一点。另一方面,Txt文件是纯文本文件,这就是为什么您对这个文件没有问题的原因


为了实现验证,您可以使用File静态类并检查文件的扩展名,然后决定下一步要做什么。您还可以使用System.IO.Path.GetExtension方法,该方法接受文件名并为您提供扩展名。

在.doc文件中搜索会有点困难,因为doc文件包含标记,以便您能够装饰文本(使用不同的字体、粗体、斜体、边距等)。有第三方库和产品可能会帮助您实现这一点。另一方面,Txt文件是纯文本文件,这就是为什么您对这个文件没有问题的原因


为了实现验证,您可以使用File静态类并检查文件的扩展名,然后决定下一步要做什么。您还可以使用System.IO.Path.GetExtension方法,该方法接受文件名并提供扩展名。

我不确定您到底在找什么。以下是一些建议:

1) 要在OpenFileDialog中仅显示.txt文件,请执行以下操作:

参考:

2) 要查看文件是否具有.txt扩展名,请使用String.EndsWith():

参考:

3) 要调用.txt文件类型的默认程序,请使用ShellExec():

参考:


“希望有帮助

我不确定你到底在找什么。以下是一些建议:

1) 要在OpenFileDialog中仅显示.txt文件,请执行以下操作:

参考:

2) 要查看文件是否具有.txt扩展名,请使用String.EndsWith():

参考:

3) 要调用.txt文件类型的默认程序,请使用ShellExec():

参考:


“希望有帮助

要搜索Word文件,您需要以下代码:

首先参考Microsoft 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的

来搜索Word文件,您将需要以下代码:

首先参考Microsoft 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的参数

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

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

   System.Diagnostics.Process.Start(info);
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);