C#起步--第一个WinForm程序

       假期无聊,就想着学习一下C#,研究下visual  studio 2010。

       经过一周的学习,对C#的基本语法和一些应用有了浅显的了解,可以写出简单的小程序。

      

       下面是一个读取文件,并显示文件基本信息和文件内容的小程序。




虽然比较简单,也没有什么太多的技术含量,但毕竟是我的起步,记录一下吧!

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GetFileInformation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "")
            {
                MessageBox.Show("请输入文件路径!");
                return;
            }
            FileInfo fi = new FileInfo(textBox1.Text);
            if (!fi.Exists)
            {
                label4.Text = "该文件不存在!";
                label2.Visible = false;
                label3.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
            }
            else
            {
                label4.Text = "该文件存在!";
                textBox2.AppendText("文件所在位置:"+ fi.DirectoryName+"\n");
                textBox2.AppendText("文件大小:    " + Convert.ToString(fi.Length) + "\n");
                textBox2.AppendText("文件创建时间:" + Convert.ToString(fi.CreationTime) + "\n");
                textBox2.AppendText("最近访问时间:" + Convert.ToString(fi.LastAccessTime) + "\n");
                textBox2.AppendText("最近修改时间:" + Convert.ToString(fi.LastWriteTime) + "\n");
                textBox2.AppendText("文件属性:    " + Convert.ToString(fi.Attributes) + "\n");

                StreamReader reader = File.OpenText(textBox1.Text);
                string s = reader.ReadLine();
                while (s != null)
                {
                    textBox3.AppendText(s+"  ");
                    s = reader.ReadLine();
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值