Streamreader问题

Zbo*_*one 2 .net c#

我是C#的一个真正的小伙子试图根据我的一个朋友在他的一个应用程序中使用的短代码编写一个小的XML替代程序.

我遇到这条线路有问题:

StreamReader sr = new StreamReader(textBox1.Text);
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:"空路径名称不合法." 为什么这不起作用?

代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        StreamReader sr = new StreamReader(textBox1.Text);
        StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
        string cur = "";
        do
        {
            cur = sr.ReadLine();
            cur = cur.Replace(textBox2.Text, textBox3.Text);
            sw.WriteLine(cur);
        }
        while (!sr.EndOfStream);

        sw.Close();
        sr.Close();

        MessageBox.Show("Finished, the new file is in the same directory as the old one");
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }


    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            this.textBox1.Text = openFileDialog1.FileName;
        }

    }


}
}
Run Code Online (Sandbox Code Playgroud)

提前致谢 :)

Kir*_*huk 8

这是因为您textBox1创建时不包含文本StreamReader.您设置了textBox1文本button1_Click,因此您必须StreamReader在该方法中创建.