天津理工大学实验报告
学院(系)名称:计算机与通信工程学院
第二篇:天津理工大学 C#实验3
实验报告
学院(系)名称:计算机与通信工程学院
【实验过程记录(源程序、测试用例、测试结果及心得体会等)】
一、文件操作
1、判断文件是否存在
using System.IO;
namespace FProject
{
class Program
{
static void Main(string[] args)
{
string path1 = @"d:\大三课程\MyTest1.txt";
if (File.Exists(path1))
{
Console.WriteLine("存在“{0}”文件", path1);
}
else
{
Console.WriteLine("“{0}”文件不存在", path1);
}
Console.ReadLine();
}
}
}
2、复制文件
public Form1()
{
InitializeComponent();
string path = @"D:\大三课程\TestFile1.txt";
if (!File.Exists(path))
{
File.AppendAllText(path, "abcd");
}
textBoxSourcePath.Text = path;
textBoxTargetDir.Text = @"D:\大三课程";
}
private void buttonFileBrower_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"D:\大三课程";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBoxSourcePath.Text = ofd.FileName;
}
}
private void buttonDirBrower_Click(object sender, EventArgs e)
{
FolderBrowserDialog fdb = new FolderBrowserDialog();
if (fdb.ShowDialog() == DialogResult.OK)
{
textBoxTargetDir.Text = fdb.SelectedPath;
}
}
private void buttonCopy_Click(object sender, EventArgs e)
{
if (File.Exists(textBoxTargetFile.Text))
{
if (MessageBox.Show(
string.Format("目标文件“{0}”已存在,覆盖吗?", textBoxTargetFile.Text), "",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)!=DialogResult.Yes)
{
return;
}
}
string dir = Path.GetDirectoryName(textBoxTargetFile.Text);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Copy(textBoxSourcePath.Text,textBoxTargetFile.Text,true);
MessageBox.Show("复制完毕!");
}
private void buttonShow_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.InitialDirectory = Path.GetDirectoryName(textBoxTargetFile.Text);
fd.ShowDialog();
}
private void textBoxTargetDir_TextChanged(object sender, EventArgs e)
{
textBoxTargetFile.Text = textBoxTargetDir.Text + "\\" + Path.GetFileName(textBoxSourcePath.Text);
}
3、删除文件
private void buttonDelete_Click(object sender, EventArgs e)
{
string dir = Path.GetDirectoryName(textBoxSourcePath.Text);
File.Delete("d://c.txt");
MessageBox.Show("删除完毕!");
}
4、移动文件
string dir = Path.GetDirectoryName(textBoxSourcePath.Text);
File.Move("d://c.txt","d://为移动文件自行创建的文件");
MessageBox.Show("移动完毕!");
二、目录操作
1、创建目录
private void buttonCreateDir_Click(object sender, EventArgs e)
{
Directory.CreateDirectory("d:\\test");
MessageBox.Show("目录创建完毕!");
}
2、目录删除
private void buttonDeleteDir_Click(object sender, EventArgs e)
{
Directory.Delete("d:\\test");
MessageBox.Show("目录创建完毕!");
}
3、目录移动
private void buttonMoveDir_Click(object sender, EventArgs e)
{
Directory.Move("d:\\test","d:\\test1");
MessageBox.Show("目录移动完毕!");
}
三、文件读写操作
//ReadAllLines方法打开一个文本文件,将文件的所有行都读入一个字符串数组,然后关闭该文件。
//WriteAllLines方法创建一个新文件,在其中写入指定的字符串数组,然后关闭文件.如果目标已存在,则覆盖该文件。
string path = @"d:\大三课程\MyTest.txt";
if(File.Exists(path))
{
File.Delete(path);
}
string[] appendText = { "学号", "姓名", "成绩" };
File.WriteAllLines(path, appendText, Encoding.Default);
string[] readText = File.ReadAllLines(path, Encoding.Default);
Console.WriteLine(string.Join(Environment.NewLine, readText));