gif放网站有锯齿,厦门市同安区建设局网站,网站备案帐号是什么意思,做影视网站用主机还是用服务器目录
一、OpenFileDialog基本属性
二、使用 OpenFile 从筛选的选择中打开文件
1.示例源码
2.生成效果
3. 其它示例
三、使用 StreamReader 以流的形式读取文件
1.示例源码
2.生成效果
四、一种新颖的Windows窗体应用文件设计方法 在C#中#xff0c;OpenFileDialog控件…目录
一、OpenFileDialog基本属性
二、使用 OpenFile 从筛选的选择中打开文件
1.示例源码
2.生成效果
3. 其它示例
三、使用 StreamReader 以流的形式读取文件
1.示例源码
2.生成效果
四、一种新颖的Windows窗体应用文件设计方法 在C#中OpenFileDialog控件用于创建一个打开文件对话框允许用户选择文件。OpenFileDialog提供了一种简单的方式来让用户选择一个或多个文件并获取用户所选文件的路径。 OpenFileDialog是打开文件对话框的意思即在窗体设计中如果需要打开本地文件就需要用到该类。
一、OpenFileDialog基本属性
属性说明InitialDirectory对话框的初始目录Filter获取或设置当前文件名筛选器字符串例如“文本文件(.txt)|.txt|所有文件(.)||.”FilterIndex在对话框中选择的文件筛选器的索引如果选第一项就设为1RestoreDirectory控制对话框在关闭之前是否恢复当前目录FileName第一个在对话框中显示的文件或最后一个选取的文件Title将显示在对话框标题栏中的字符AddExtension是否自动添加默认扩展名CheckPathExists在对话框返回之前检查指定路径是否存在DefaultExt默认扩展名DereferenceLinks在从对话框返回前是否取消引用快捷方式ShowHelp启用帮助按钮ValiDateNames控制对话框检查文件名中是否不含有无效的字符或序列
二、使用 OpenFile 从筛选的选择中打开文件
1.示例源码
//使用 OpenFile 从筛选的选择中打开文件
using System.Diagnostics;
using System.Security;namespace WinFormsApp1
{public partial class OpenFileDialogForm : Form{private readonly Button selectButton;private readonly OpenFileDialog openFileDialog1;public OpenFileDialogForm(){InitializeComponent();//新建openFileDialog控件openFileDialog1 new OpenFileDialog(){FileName Select a text file, //OpenFileDialog窗体提示Filter Text files (*.txt)|*.txt, //选择什么扩展名类型的文件Title Open text file //OpenFileDialog窗体的抬头};//新建按钮及点击事件selectButton new Button(){Size new Size(100, 20),Location new Point(15, 15),Text Select file};selectButton.Click new EventHandler(SelectButton_Click);Controls.Add(selectButton);}/// summary/// 按钮点击事件应用/// 使用 Button 控件的 Click 事件处理程序打开包含仅显示文本文件的筛选器的 OpenFileDialog。 /// 用户选择文本文件并选择“确定”后可用 OpenFile 方法在记事本中打开该文件/// /summaryprivate void SelectButton_Click(object? sender, EventArgs e){if (openFileDialog1.ShowDialog() DialogResult.OK){try{var filePath openFileDialog1.FileName;using Stream str openFileDialog1.OpenFile();Process.Start(notepad.exe, filePath);}catch (SecurityException ex){MessageBox.Show($Security error.\n\nError message: {ex.Message}\n\n $Details:\n\n{ex.StackTrace});}}}}
}2.生成效果 3. 其它示例 在作者的这篇文章中也有这种读取文件的示例。 写文章-CSDN创作中心 https://mp.csdn.net/mp_blog/creation/editor/134621313
三、使用 StreamReader 以流的形式读取文件
1.示例源码
//使用 StreamReader 以流的形式读取文件
using System.Security;
namespace _05_3
{public partial class Form1 : Form{private readonly Button selectButton;private readonly OpenFileDialog openFileDialog1;private readonly TextBox textBox1;public Form1(){InitializeComponent();//创建OpenFileDialog控件openFileDialog1openFileDialog1 new OpenFileDialog();//创建按钮控件selectButton及添加点击事件selectButton new Button{Size new Size(100, 20),Location new Point(15, 15),Text Select file};selectButton.Click new EventHandler(SelectButton_Click);//创建文本框控件textBox1textBox1 new TextBox{Size new Size(300, 300),Location new Point(15, 40),Multiline true,ScrollBars ScrollBars.Vertical};//设置Form1表格大小ClientSize new Size(330, 360);Controls.Add(selectButton);Controls.Add(textBox1);}//自定义方法private void SetText(string text){textBox1.Text text;}/// summary/// 使用 StreamReader 以流的形式读取文件/// 使用 Windows 窗体 Button 控件的 Click 事件处理程序通过 ShowDialog 方法打开 OpenFileDialog。/// 用户选择一个文件并选择“确定”后StreamReader 类的实例将读取该文件并在窗体的文本框中显示文件内容。/// /summaryprivate void SelectButton_Click(object? sender, EventArgs e){if (openFileDialog1.ShowDialog() DialogResult.OK){try{var sr new StreamReader(openFileDialog1.FileName);SetText(sr.ReadToEnd());}catch (SecurityException ex){MessageBox.Show($Security error.\n\nError message: {ex.Message}\n\n $Details:\n\n{ex.StackTrace});}}}}
}2.生成效果 四、一种新颖的Windows窗体应用文件设计方法 这两个示例使用了一种Windows窗体应用文件新的设计方法不设计Form1.cs[设计]。所有试图、控件都通过编程实现。是不是很新颖呢你更喜欢哪一种设计方法呢