C# OpenFileDialog 不显示内容,显示文件目录

标签 c# visual-c#-express-2010

所以对于我的第一个 c# 程序,我想制作一个机器人。 我已经制作了一些 GUI,但一切都没有按我想要的方式定位。

每当我尝试使用 OpenFileDialog 加载文件中没有特定内容的文本文件时,它会在富文本框中显示目录而不是文件的实际内容。

图形用户界面:http://puu.sh/5kLK6.png

加载文件时,我得到的不是实际内容:http://puu.sh/5kLL2.png

文件的实际内容是“wepufhwoighwiar” 加载代理按钮代码:

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Browse Text Files";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                proxieslist.Text = openFileDialog1.FileName;
            }
        }

最佳答案

OpenFileDialog 不会为您打开文件。它只是帮助您选择要打开的文件。要打开文件,您必须使用命名空间 System.IO 中的一些类。这是读取所有文本(明文)的简单代码:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   proxieslist.Text = System.IO.File.ReadAllText(openFileDialog1.FileName);
}

关于C# OpenFileDialog 不显示内容,显示文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027860/

相关文章:

c# - 如何动态添加选择器

c# - 将空格附加到 stringbuilder?

c# - 错误 : The type or namespace AdvancedTextBoundary could not be found

c# - 加载带有大量控件的繁重 UI 的最佳方式

c# - 如何在 Xamarin.Mac Cocoa 应用程序中禁用窗口大小调整?

c# - 允许 Form1 关闭而不关闭 C# 中的应用程序?

c# - 在可视化 C# 中扩展标签?

c# - 在 C# 中删除 datagridview 中的选定行?

c# - 正确处理和删除对 UserControl 的引用,以避免内存泄漏

c# - 如何区分类型 : Int32[] & Int32[*]?