当前位置: 首页 > article >正文

C# Windows Forms应用程序-002

目录

项目结构

主类和命名空间  

构造函数和析构函数  

初始化组件 (InitializeComponent)

按钮点击事件处理程序

主程序入口点

项目截图:

完整代码:


项目结构

这个项目是一个简单的C# Windows Forms应用程序,获取指定文件的根信息,包含一个主窗体 Form1 和一些控件(标签、文本框、按钮等)。整个项目的主要功能是让用户选择文件或文件夹,并提供了一些关于这些路径的信息。

主类和命名空间  

        命名空间: Files  

        类: Form1 继承自 System.Windows.Forms.Form  

成员变量  

        Label label1: 显示提示信息的标签。  

        TextBox textBox1: 用户输入或显示文件/文件夹路径的文本框。  

        Button button1, button2, button3, button4: 四个按钮分别用于不同的操作。         

        FolderBrowserDialog folderBrowserDialog1: 文件夹浏览对话框。  

        OpenFileDialog openFileDialog1: 文件打开对话框。  

        IContainer components: 窗体设计器使用的容器对象。

构造函数和析构函数  

        构造函数: 调用 InitializeComponent() 方法来初始化窗体及其控件。  

        析构函数: 如果 components 不为空,则释放其资源。  

初始化组件 (InitializeComponent)

        这部分代码由Windows Forms设计器生成,主要负责设置各个控件的位置、大小和其他属性。例如:  

                设置 label1 的位置、大小和文本内容。  

                设置 textBox1 的初始值为 "C:\\Windows"。  

                设置每个按钮的位置、大小和点击事件处理程序。  

按钮点击事件处理程序

        button1_Click:当用户点击“浏览文件夹”按钮时,会弹出一个文件夹选择对话框。如果用户选择了某个文件夹并确认,所选文件夹的路径会被填入 textBox1 中。

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

button2_Click:当用户点击“浏览文件”按钮时,会弹出一个文件打开对话框。如果用户选择了某个文件并确认,所选文件的路径会被填入 textBox1 中。

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

button3_Click:当用户点击“显示根信息”按钮时,会获取当前路径所在的驱动器信息,并通过消息框显示出来。这包括驱动器类型、总空间和可用空间。

private void button3_Click(object sender, EventArgs e)
{string path = textBox1.Text;try{DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(path));MessageBox.Show( $ "驱动器类型: {driveInfo.DriveType}" + $ "总空间: {driveInfo.TotalSize} bytes
" +$ "可用空间: {driveInfo.AvailableFreeSpace} bytes");}catch (Exception ex){MessageBox.Show("无法获取驱动器信息: " + ex.Message);}
}

button4_Click:当用户点击“父目录信息”按钮时,会将 textBox1 中的路径更改为该路径的父目录路径。如果已经是根目录,则弹出提示信息。

private void button4_Click(object sender, EventArgs e)
{string path = textBox1.Text;try{string parentDir = Path.GetDirectoryName(path);if (!string.IsNullOrEmpty(parentDir)){textBox1.Text = parentDir;}else{MessageBox.Show("当前路径已是根目录或无效路径。");}}catch (Exception ex){MessageBox.Show("无法获取父目录信息: " + ex.Message);}
}

主程序入口点

  • 在 Program 类中定义了静态方法 Main,这是应用程序的入口点。调用 Application.Run(new Form1()) 来启动应用程序并显示主窗体 Form1
static class Program
{[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}
}

项目截图:

完整代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Windows.Forms;namespace Files
{public class Form1 : System.Windows.Forms.Form{private Label label1;private TextBox textBox1;private Button button1;private FolderBrowserDialog folderBrowserDialog1;private Button button2;private Button button3;private OpenFileDialog openFileDialog1;private Button button4;private IContainer components = null;public Form1(){InitializeComponent();}protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码private void InitializeComponent(){this.label1 = new Label();this.textBox1 = new TextBox();this.button1 = new Button();this.folderBrowserDialog1 = new FolderBrowserDialog();this.button2 = new Button();this.button3 = new Button();this.openFileDialog1 = new OpenFileDialog();this.button4 = new Button();this.SuspendLayout();// label1this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(15, 22);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(159, 13);this.label1.TabIndex = 0;this.label1.Text = "文件或文件夹全路径名:";// textBox1this.textBox1.Location = new System.Drawing.Point(17, 57);this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(320, 20);this.textBox1.TabIndex = 1;this.textBox1.Text = @"C:\Windows";// button1this.button1.Location = new System.Drawing.Point(17, 104);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(80, 23);this.button1.TabIndex = 2;this.button1.Text = "浏览文件夹";this.button1.Click += new EventHandler(this.button1_Click);// button2this.button2.Location = new System.Drawing.Point(97, 104);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(80, 23);this.button2.TabIndex = 5;this.button2.Text = "浏览文件";this.button2.Click += new EventHandler(this.button2_Click);// button3this.button3.Location = new System.Drawing.Point(177, 104);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(80, 23);this.button3.TabIndex = 6;this.button3.Text = "显示根信息";this.button3.Click += new EventHandler(this.button3_Click);// button4this.button4.Location = new System.Drawing.Point(257, 104);this.button4.Name = "button4";this.button4.Size = new System.Drawing.Size(80, 23);this.button4.TabIndex = 7;this.button4.Text = "父目录信息";this.button4.Click += new EventHandler(this.button4_Click);// Form1this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);this.AutoScaleMode = AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(400, 175);this.Controls.Add(this.button4);this.Controls.Add(this.button3);this.Controls.Add(this.button2);this.Controls.Add(this.button1);this.Controls.Add(this.textBox1);this.Controls.Add(this.label1);this.FormBorderStyle = FormBorderStyle.FixedSingle;this.MaximizeBox = false;this.MinimizeBox = false;this.Name = "Form1";this.StartPosition = FormStartPosition.CenterScreen;this.Text = "文件操作示例";this.ResumeLayout(false);this.PerformLayout();}#endregionprivate void button1_Click(object sender, EventArgs e){if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){textBox1.Text = folderBrowserDialog1.SelectedPath;}}private void button2_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() == DialogResult.OK){textBox1.Text = openFileDialog1.FileName;}}private void button3_Click(object sender, EventArgs e){string path = textBox1.Text;try{DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(path));MessageBox.Show($"驱动器类型: {driveInfo.DriveType}\n" +$"总空间: {driveInfo.TotalSize} bytes\n" +$"可用空间: {driveInfo.AvailableFreeSpace} bytes");}catch (Exception ex){MessageBox.Show("无法获取驱动器信息: " + ex.Message);}}private void button4_Click(object sender, EventArgs e){string path = textBox1.Text;try{string parentDir = Path.GetDirectoryName(path);if (!string.IsNullOrEmpty(parentDir)){textBox1.Text = parentDir;}else{MessageBox.Show("当前路径已是根目录或无效路径。");}}catch (Exception ex){MessageBox.Show("无法获取父目录信息: " + ex.Message);}}}static class Program{[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}
}

这是个简单的Windows Forms应用程序展示了如何使用基本的Windows Forms控件(如标签、文本框、按钮)来构建一个交互式界面。通过按钮点击事件处理程序,实现了文件夹和文件的选择,以及获取和显示路径相关信息的功能。

http://www.lryc.cn/news/2385908.html

相关文章:

  • 理解计算机系统_线程(八):并行
  • 【MySQL】09.索引
  • 【备忘】 windows 11安装 AdGuardHome,实现开机自启,使用 DoH
  • [Windows] 游戏常用运行库- Game Runtime Libraries Package(6.2.25.0409)
  • MYSQL order 、group 与row_number详解
  • QT之巧用对象充当信号接收者
  • 《红警2000》游戏信息
  • Vue3 + ThinkPHP8 + PHP8.x 生态与 Swoole 增强方案对比分析
  • (九)PMSM驱动控制学习---高阶滑膜观测器
  • 25年上半年五月之软考之设计模式
  • Mongo DB | 多种修改数据库名称的方式
  • QListWidget的函数,信号介绍
  • Python类属性与实例属性的覆盖机制:从Vector2d案例看灵活设计
  • QML与C++交互2
  • EtherNet/IP机柜内解决方案在医疗控制中心智能化的应用潜能和方向分析
  • springboot中各模块间实现bean之间互相调用(service以及自定义的bean)
  • RabbitMQ 可靠性保障:消息确认与持久化机制(二)
  • QML学习07Property
  • Skywalking安装部署使用教程
  • 网络编程与axios技术
  • 【结构设计】以3D打印举例——持续更新
  • MySQL中的重要常见知识点(入门到入土!)
  • 理解全景图像拼接
  • 云原生安全基石:Linux进程隔离技术详解
  • 基于PySide6与pycatia的CATIA几何阵列生成器开发实践
  • Linux学习心得问题总结(三)
  • 蓝桥杯国14 不完整的算式
  • Anthropic推出Claude Code SDK,强化AI助理与自动化开发整合
  • 6.4.1最小生成树
  • DAY 33