c#代码—求解释
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using
namespace HelloWorld
{
public partial class Form1 : Form
{
private string FileName="a.txt";
private string FileContent;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileInfo info = new FileInfo(FileName);
if (!info.Exists)
{
MessageBox.Show("File Does't Exist!Will Create!");
}
FileStream fs=info.Open(FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite);
StreamReader sr=new StreamReader(fs);
string strContent=sr.ReadLine();
sr.Close();
fs.Close();
this.textContent.Text=strContent;
this.FileContent=strContent;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ArrayList listContent = new ArrayList();
//将fileContent按照空格或者制表符分割成字符串组
string[] strContent = FileContent.Split(null);
foreach (string str in strContent)
{
if (str != "")
{
listContent.Add(str);
}
}
string strDisplay = "";
foreach (object obj in listContent)
{
string str = obj as string;
if (str != null)
{
strDisplay += str + System.Environment.NewLine;
}
}
MessageBox.Show(strDisplay);
}
//右键
else
{
Hashtable ht =new Hashtable();
//将FileContent按照空格或者制表符分割成字符串数组
string[] strContent=FileContent.Split(null);
int number=0;
foreach(string str in strContent)
{
if(str!="")
{
ht.Add("Number" + number.ToString(), str);
}
number++;
}
string strDisplay="";
IDictionaryEnumerator enumer=ht.GetEnumerator();
while(enumer.MoveNext()){
strDisplay+=enumer.Key.ToString()+":"+enumer.Value.ToString()+System.Environment.NewLine;
}
MessageBox.Show(strDisplay);
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(FileName);
if (!info.Exists)
{
MessageBox.Show(" File Doesn't Exist! Will Create!");
}
FileStream fs = info.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(FileContent);
sw.Close();
fs.Close();
}
private void btnLoadFile_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(FileName);
if (!info.Exists)
{
MessageBox.Show(" File Doesn't Exist! Will Create!");
}
FileStream fs = info.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
string strContent = sr.ReadLine();
sr.Close();
fs.Close();
MessageBox.Show("File Content is:" + strContent);
}
private void btnException_Click(object sender, EventArgs e)
{
throw new Exception("I throw an Exceotion,but i don't konw what is to do ,hehe");
}
private void textContent_TextChanged(object sender, EventArgs e)
{
this.FileContent = this.textContent.Text;
}
}
}