假期无聊,就想着学习一下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(); } } } } }