单个文件复制和目录文件复制
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 fileexercise
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* 复制单个文件
if (File.Exists(@"D:\23.txt"))
{
if (MessageBox.Show("目标文件已存在,是否覆盖?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
File.Copy(@"D:\GL\23.txt", @"D:\23.txt", true);
}
}
else
{
File.Copy(@"D:\GL\23.txt", @"D:\23.txt", true);
}
*/
//显示对话框,返回值为DialogueResult类型,如果是OK表明选择的是打开,否则是取消;
// openFileDialog1.ShowDialog();
//MessageBox.Show(openFileDialog1.FileName);
/*打开文件目录,设置要保存的文件目录
* openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "可执行文件|*.exe|文本文件|*.txt|所有文件|*.*";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
File.Copy(openFileDialog1.FileName,saveFileDialog1.FileName);
}
}
*/
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog sourcefolder = new FolderBrowserDialog();
string sDir, dDir;
sourcefolder.Description = "源文件路径";
if (sourcefolder.ShowDialog() == DialogResult.OK)
{
sDir = sourcefolder.SelectedPath;
sourcefolder.Description = "目的路径";
if (sourcefolder.ShowDialog() == DialogResult.OK)
{
dDir = sourcefolder.SelectedPath;
String[] files = Directory.GetFiles(sDir);
foreach (string filepath in files)
{
string DDir =dDir+ "\\" + filepath.Substring(filepath.LastIndexOf("\\") + 1);
File.Copy(filepath, DDir);
}
}
}
}
}
}
在Windowsform中创建一个记事本读取一个文件:
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 记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
txt.Text = "";
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开文件";
ofd.Filter = "文本文件|*.txt|所有文件|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
//点击了对话框的开始按钮才读入文件
//读入文件第一步,声明一个文件流,在内存和文件中建立桥梁(filemode 描述文件流功能)(Filemode 是open就是要读文件,fileaccess就要选read,fielshare 其他进程对该文件是否可访问)
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.None);
//第二步,创建读写器,这里创建读取器
StreamReader sr = new StreamReader(fs,Encoding.Default);
while (!sr.EndOfStream)
//sr.EndOfStream //指示是否读到结尾
{
string line = sr.ReadLine(); //不返回换行符,所以要自己加
txt.Text = txt.Text + line + "\r\n";
}
//读操作
//sr.ReadLine();//读取一行
//txt.Text = sr.ReadToEnd();//读取到结尾
//第四步,关闭读取器
sr.Close();
//第五步,关闭文件流对象,释放对所操作文件的锁定
fs.Close();
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}