文件操作学习
例1. 显示当前计算机上所有驱动盘的相关信息
using System.IO; //.NET类库的**System.IO**命名空间提供了完整的对文件和流的访问支持。
private void button1_Click(object sender, EventArgs e)
{
DriveInfo[] drives=DriveInfo.GetDrives();
for(int i=0; i<drives.Length; i++)
{
listBox1.Items.Add(drives[i].Name+" "+drives[i].VolumeLabel);
}
}
DriveInfo类提供了一个共有静态方法GetDrives,该方法返回一个DriveInfo[]类型的数组,可以表示当前计算机上所有的逻辑驱动盘的列表(记得要有权限o)。
VolumeLabel返回驱动盘卷标。
例2. 输出当前目录下的所有文件信息
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
for(int i=0; i<files.Length; i++)
{
listBox2.Items.Add(files[i]+" "+File.GetCreationTime(files[i]).ToLocalTime());
}
}
例3. 按字节把字符串写入文件C:\MyFile.txt
FileStream fs = File.Open("F:\\MyFile.txt ",FileMode.Append);
byte[] Info = { (byte)'a', (byte)'b', (byte)'c', (byte)'d' };
fs.Write(Info, 0, Info.Length);
fs.Close();
FileMode:该枚举类型表示文件的打开方式,可以有
- Append:以追加方式打开文件,如果文件存在则到达文件末尾,否则创建一个新文件
- Create:创建并打开一个新文件,已经存在则覆盖
- CreateNew:创建并打开,已经存在则发生异常
- Open:打开,不存在则异常
- OpenOrCreate:打开或新建,存在则打开,否则新建
- Truncate:打开现有文件,并清空文件内容
FileAccess:表示对文件的访问权限,可以有
- Read
- ReadWrite
- Write
例4. 使用StreamReader和StreamWriter进行文件拷贝
saveFileDialog1.ShowDialog();
FileStream f1 = File.Open("F:\\MyFile.txt",FileMode.Open);
FileStream f2 = File.Create(saveFileDialog1.FileName);
StreamReader sr = new StreamReader(f1);
StreamWriter sw = new StreamWriter(f2);
sw.Write(sr.ReadToEnd());
sr.Close();
sw.Close();
f1.Close();
MessageBox.Show("copy finished");
例5. 文件加密和解密
class Test
{
public static void Main()
{
string path = @"F:\MyFile.txt";
string password = "this is passwoad";
if (!File.Exists(path))
{
StreamWriter sw = File.CreateText(path);
Console.WriteLine("请输入文本内容:");
string data_before = Console.ReadLine();
char[] data_after=data_before.ToCharArray();
char[] key=password.ToCharArray();
for(int i=0; i<data_before.Length; i++)
{
data_after[i]^= key[i%key.Length];
}
sw.WriteLine(data_after);
Console.WriteLine("加密完成");
sw.Close();
}
else
{
StreamReader sr=File.OpenText(path);
string data_before = sr.ReadToEnd();
data_before=data_before.Replace( "\r\n","");
char[] data_after = data_before.ToCharArray();
char[] key = password.ToCharArray();
for (int i = 0; i < data_before.Length; i++)
{
data_after[i] ^= key[i % key.Length];
}
Console.WriteLine(data_after);
sr.Close();
File.Delete(path);
StreamWriter sw = File.CreateText(path);
sw.WriteLine(data_after);
sw.Close();
}
}
}
GDI+图形编程
例1. 使用图形与画笔类直接在窗体上绘制各种基本形状。
private void button1_Click(object sender, EventArgs e)
{
Graphics g;
g=this.CreateGraphics();
Pen pen=new Pen(Color.Black);
g.DrawLine(pen,0,Height/2,Width,Height/2);
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g=this.CreateGraphics();
Pen pen=new Pen(Color.Green);
g.DrawRectangle(pen, Width/2, Height/2, 100, 100);
}
private void button3_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Green);
g.DrawEllipse(pen, Width / 3, 0, 100, 200);
}
}
画直线
用Graphics的DrawLine方法
public void DrawLine(Pen pen,int x1,int y1,int x2,int y2);
表示在(x1,y1)和(x2,y2)之间画一条直线
public void DrawLine(Pen pen,Point pt1,Point pt2);
表示在pt1和pt2两点之间画一条直线
画矩形
用Graphics的DrawRectangle方法
public void DrawRectangle(Pen pen,int x1,int y1,int width,int height);
绘制一个由左上角坐标,宽度和高度指定的矩形
public void DrawRectangle(Pen pen,Rectangle rect);
绘制一个矩形,rect为要绘制矩形的结构
画椭圆
用Graphics的DrawEllipse方法
public void DrawEllipse(Pen pen,int x1,int y1,int width,int height);
绘制一个自由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆
x,y分别定义椭圆的外界边框左上角的横坐标和纵坐标,width定义椭圆的边框的宽度
public void DrawEllipse(Pen pen,Rectangle rect)
rect为Rectangle结构型数据
例2. 在窗体上为各种形状填充不同的颜色。
本例的目标是使用图形与画刷类为窗体上绘制的基本形状填充颜色。
Graphics g = this.CreateGraphics();
Brush brush=new SolidBrush(Color.Pink);
Point p1=new Point(Width/2,0);
Point p2=new Point(Width/3,0);
Size rect1=new Size(100,50);
Size rect2=new Size(100,200);
Rectangle r = new Rectangle(p1, rect1);
Rectangle r2=new Rectangle(p2, rect2);
g.FillRectangle(brush,r);
g.FillEllipse(Brushes.Red,r2);
例3. 绘制正弦函数
private void button5_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen=new Pen(Color.Black);
Point o=new Point(50,Height/2);
Point ro = new Point(Width, Height / 2);
g.DrawLine(pen, 50, 20, 50, Height);
g.DrawLine(pen, o,ro);
Point lastP=new Point(50,Height/2);
for(int i = 50; i < Width; i++)
{
Point nextP=new Point(i+1+o.X,o.Y-(int)(o.Y*Math.Sin((i+1)*6.28/(Width-50)))); //6.28是2派的意思
g.DrawLine(pen,lastP,nextP);
lastP =nextP;
}
}
画出来是在x在2派区间内的,x范围可以任意改,波形图也可以任意改。
例4. 按百分比绘制饼图
参数解释:brush:确定填充特性的brush对象 x:边框左上角的x坐标 y:边框左上角的y坐标 width:边框的宽度 height:边框的高度 startAngle:从x轴沿顺时针方向转到扇形区第一个边所测得的角度 sweepAngle:从startAngle参数沿顺时针方向旋转到扇形区第二个边所测得的角度(以度为单位)
数据库应用
和mysql建立连接,使用第三方控件Mysql.Data.dll,然后在项目中添加该控件的引用。
下载地址:http://dev.mysql.com/downloads/connector/net/
然后在代码里输入using MySql.Data.MySqlClient; 就ok啦
step1.连接数据库
string connstr = "server=127.0.0.1;port=3306;user=root;password=root;database=gongyin";
MySqlConnection conn=new MySqlConnection(connstr);
try
{
conn.Open();
MessageBox.Show("连接成功");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
step2.查询 条件查询
private void button6_Click(object sender, EventArgs e)
{
string connstr = "server=127.0.0.1;port=3306;user=root;password=root;database=gongyin";
MySqlConnection conn=new MySqlConnection(connstr);
try
{
conn.Open();
// MessageBox.Show("连接成功");
string mysql = "select * from spj";
MySqlCommand cmd = new MySqlCommand(mysql,conn);
MySqlDataReader dr = cmd.ExecuteReader(); 执行ExecuteReader()返回一个MySqlDataReader对象
while (dr.Read())
{
listBox1.Items.Add(dr.GetString("sno")+" "+dr.GetString("pno")+" "+dr.GetString("jno"));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
step3. 查询2.0 条件查询(查询(至少)供应螺母零件、但不供应齿轮的供应商名。)
string mysql2 = "select distinct sname from s,spj,p where s.sno=spj.sno and p.pno=spj.pno and pname = '螺母' and spj.pno not in (select spj.pno from p,spj where p.pno = spj.jno and pname = '齿轮'); ";
MySqlCommand cmd2=new MySqlCommand(mysql2,conn);
MySqlDataReader dr2=cmd2.ExecuteReader();
while (dr2.Read())
{
listBox1.Items.Add(dr2.GetString("sname"));
}
step3. 查询3.0 结果返回一个值
string mysql = "select count(*) from spj";
MySqlCommand cmd=new MySqlCommand(mysql,conn);
object result= cmd.ExecuteScalar();//执行查询,并返回查询结果集中第一行的第一列。所有其他的列和行将被忽略。select语句无记录返回时,ExecuteScalar()返回NULL值
if (result != null)
{
int count=int.Parse(result.ToString());
listBox1.Items.Add(count);
}
ExecuteReader——用于数据库的查询。查询结果是返回MySqlDataReader对象,MysqlDataReader包含sql语句执行的结果,并提供一个方法从结果中阅读一行。
ExecuteNonQuery——用于插入、更新和删除数据。
ExecuteSchalar——用于查询数据时,返回查询结果集中第一行第一列的值,即只返回一个值。