分享
 
 
 

最简单的IO操作

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

using System;

using System.Drawing;

//using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;

namespace fso_cs

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 :System.Windows.Forms.Form

{

#region Static String to Get Where Is The %WinDir%

string winDir=System.Environment.GetEnvironmentVariable("windir");

#endregion

#region Windows Form Designer generated code

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.Button button4;

private System.Windows.Forms.Button button5;

private System.Windows.Forms.Button button6;

private System.Windows.Forms.ListBox listbox1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support.

//

InitializeComponent();

//

// TO DO:Add any constructor code after InitializeComponent call.

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.button3 = new System.Windows.Forms.Button();

this.listbox1 = new System.Windows.Forms.ListBox();

this.button4 = new System.Windows.Forms.Button();

this.button5 = new System.Windows.Forms.Button();

this.button6 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(216, 32);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(112, 23);

this.button1.TabIndex = 1;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(216, 64);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(112, 23);

this.button2.TabIndex = 2;

this.button2.Text = "button2";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// button3

//

this.button3.Location = new System.Drawing.Point(216, 96);

this.button3.Name = "button3";

this.button3.Size = new System.Drawing.Size(112, 23);

this.button3.TabIndex = 3;

this.button3.Text = "button3";

this.button3.Click += new System.EventHandler(this.button3_Click);

//

// listbox1

//

this.listbox1.Location = new System.Drawing.Point(24, 24);

this.listbox1.Name = "listbox1";

this.listbox1.Size = new System.Drawing.Size(176, 199);

this.listbox1.TabIndex = 0;

//

// button4

//

this.button4.Location = new System.Drawing.Point(216, 128);

this.button4.Name = "button4";

this.button4.Size = new System.Drawing.Size(112, 23);

this.button4.TabIndex = 4;

this.button4.Text = "button4";

this.button4.Click += new System.EventHandler(this.button4_Click);

//

// button5

//

this.button5.Location = new System.Drawing.Point(216, 160);

this.button5.Name = "button5";

this.button5.Size = new System.Drawing.Size(112, 23);

this.button5.TabIndex = 5;

this.button5.Text = "button5";

this.button5.Click += new System.EventHandler(this.button5_Click);

//

// button6

//

this.button6.Location = new System.Drawing.Point(216, 192);

this.button6.Name = "button6";

this.button6.Size = new System.Drawing.Size(112, 23);

this.button6.TabIndex = 6;

this.button6.Text = "button6";

this.button6.Click += new System.EventHandler(this.button6_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(360, 273);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button6,

this.button5,

this.button4,

this.button3,

this.button2,

this.button1,

this.listbox1});

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

#region The Main Funcation

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

#endregion

#region The Function that used in the program

/// <summary>

/// To add files in the windir into listbox1

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button6_Click(object sender, System.EventArgs e)

{

//How to obtain list of files (example uses Windows folder).

this.listbox1.Items.Clear();

string[] files= Directory.GetFiles(winDir);

foreach (string i in files)

{

addListItem(i);

}

}

/// <summary>

/// To read the system.ini file in the windir

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button1_Click(object sender, System.EventArgs e)

{

//How to read a text file.

//try...catch is to deal with a 0 byte file.

this.listbox1.Items.Clear();

StreamReader reader=new StreamReader(winDir + "\system.ini");

try

{

do

{

addListItem(reader.ReadLine());

}

while(reader.Peek() != -1);

}

catch

{

addListItem("File is empty");}

finally

{

reader.Close();}

}

/// <summary>

/// To give the buttons text

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Form1_Load(object sender, System.EventArgs e)

{

this.button1.Text = "Read Text File";

this.button2.Text = "Write Text File";

this.button3.Text = "View File Information";

this.button4.Text = "List Drives";

this.button5.Text = "List Subfolders";

this.button6.Text = "List Files";

}

/// <summary>

/// To get the folder in the windir

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button5_Click(object sender, System.EventArgs e)

{

//How to get a list of folders (example uses Windows folder).

this.listbox1.Items.Clear();

string[] dirs = Directory.GetDirectories(winDir);

foreach(string dir in dirs)

{

addListItem(dir);

}

}

/// <summary>

/// TO get LogicalDrives

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button4_Click(object sender, System.EventArgs e)

{

//Demonstrates how to obtain a list of disk drives.

this.listbox1.Items.Clear();

string[]drives = Directory.GetLogicalDrives();

foreach(string drive in drives)

{

addListItem(drive);

}

}

/// <summary>

/// To get a file's properties

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button3_Click(object sender, System.EventArgs e)

{

//How to retrieve file properties (example uses Notepad.exe).

this.listbox1.Items.Clear();

FileInfo FileProps =new FileInfo(winDir + "\notepad.exe");

addListItem("File Name = " + FileProps.FullName);

addListItem("Creation Time = " + FileProps.CreationTime);

addListItem("Last Access Time = " + FileProps.LastAccessTime);

addListItem("Last Write TIme = " + FileProps.LastWriteTime);

addListItem("Size = " + FileProps.Length);

FileProps = null;

}

/// <summary>

/// addListItem

/// </summary>

/// <param name="value"></param>

private void addListItem(string value)

{

this.listbox1.Items.Add(value);

}

/// <summary>

/// To make a file

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button2_Click(object sender, System.EventArgs e)

{

//Demonstrates how to create and write to a text file.

StreamWriter writer = new StreamWriter("c:\\KBTest.txt");

writer.WriteLine("File created using StreamWriter class.");

writer.Close();

this.listbox1.Items.Clear();

addListItem("File Written to C:\\KBTest.txt");

}

#endregion

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有