c# - 将 opentext 更改为 opendialog

标签 c#

您好,我想使用对话框形式选择文本文件,而不必使用给定的路径。我该怎么做?

我想用 opendialog 替换 opentext?我试过了,但我想使用 streamreader 的流出现错误....

    private void button2_Click(object sender, EventArgs e)
    {

        using (StreamReader reader = File.OpenText("c:\\myparts.txt"))
        {
            label3.Text = "Ready to Insert";
            textBox7.Text = reader.ReadLine();
            textBox8.Text = reader.ReadLine();
            textBox9.Text = reader.ReadLine();
            textBox10.Text = reader.ReadLine();
}

最佳答案

你想要这样的东西吗?

OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShowDialog() == DialogResult.OK)
{
    using (var reader = File.OpenText(dlg.FileName))
    {
        ...
    }
}

关于c# - 将 opentext 更改为 opendialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21891400/

相关文章:

c# - 是否可以在 XAML 中绑定(bind) Canvas 的 Children 属性?

c# - 开始一项新 Activity

c# - 使用 Silverlight 的 Windows 应用程序

c# - Datagridview 中的文本框示例

c# - 在 Silverlight 中显示文件类型图标

c# - WebBrowser 与 WebBrowserBase 类

c# - Hololens - UserConsentVerifier 不适用于 Hololens 第一代

c# - 匹配模式后从字符串中剪切文本

c# - 在 C# 中使用 OpenGl?

c# - 通过中介路由 WCF?