且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建显示文本文件的应用程序

更新时间:2023-10-14 10:38:34

谷歌搜索出现了很多例子使用方法:



1. openFileDialog microsoft docs openFileDialog wpf示例 - Google搜索 [ ^ ]:

* WPF教程 - OpenFileDialog [ ^ ]

* OpenFileDialog示例 [ ^ ]

* 如何:使用OpenFileDialog组件打开文件| Microsoft Docs [ ^ ]



2.读取文本文件: wpf读取文本文件 - Google搜索 [ ^ ]

* 如何:读取文本f rom a File | Microsoft Docs [ ^ ]

* 如何在C#中读取文本文件? [ ^ ]

* 使用C#读取和写入文本文件押韵 [ ^ ]



来自如何:使用OpenFileDialog组件打开文件Microsoft Docs [ ^ ]:

Google search turns up lots of examples of how to use:

1. openFileDialog: microsoft docs openFileDialog wpf example - Google Search[^]:
* WPF Tutorial - The OpenFileDialog[^]
* OpenFileDialog sample[^]
* How to: Open Files Using the OpenFileDialog Component | Microsoft Docs[^]

2. Read a text file: wpf read a text file - Google Search[^]
* How to: Read Text from a File | Microsoft Docs[^]
* How to read a text file in C#?[^]
* Reading and writing to a text file with C# | Rhyous[^]

A concise solution from How to: Open Files Using the OpenFileDialog Component | Microsoft Docs[^]:
private void openFileClick(object sender, RoutedEventArgs e)
{  
   if(openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
   {  
      System.IO.StreamReader sr = new   
         System.IO.StreamReader(openFileDialog1.FileName);  
      source.Text = sr.ReadToEnd();
      sr.Close();  
   }  
}



此外,除了 wpf-tutorial .com [ ^ ]另一个有用的网站是:你应该知道的关于WPF的2000件事WPF开发人员需要知道的所有内容,在Bite-Sized Chunks中 [ ^ ]


Also, besides wpf-tutorial.com[^] another good helpful site is: 2,000 Things You Should Know About WPF | Everything a WPF Developer Needs to Know, in Bite-Sized Chunks[^]


您已创建 openFileDialogFileOk 方法,但尚未将其连接到 openFileDialog.FileOk 事件:

You've created the openFileDialogFileOk method, but you haven't wired it up to the openFileDialog.FileOk event:
public MainWindow()
{
    InitializeComponent();
    openFileDialog = new OpenFileDialog();
    openFileDialog.FileOk += openFileDialogFileOk; // <-- Add this line
}



你还应该避免在循环中使用字符串连接,因为效率非常低:


You should also avoid using string concatenation in a loop, as it's extremely inefficient:

private void openFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
    string fullPathname = openFileDialog.FileName;
    FileInfo src = new FileInfo(fullPathname);
    fileName.Text = src.FullName;
    
    source.Text = File.ReadAllText(src.FullName);
}



File.ReadAllText方法 [ ^ ]