2008年6月13日
private void button1_Click(object sender, EventArgs e)
{
//建立一个打开文件对象
OpenFileDialog ofdAim = new OpenFileDialog();
//初始首选路径
ofdAim.InitialDirectory = "c:\\";
//扩展名
ofdAim.Filter = "txt files (*.txt) |*.txt|All files (*.*)|*.*";
//如果选择了是,应该执行什么操作
if (ofdAim.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofdAim.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
//选择当前的文件夹对象
FolderBrowserDialog fbdDlg = new FolderBrowserDialog();
fbdDlg.SelectedPath = "F:\\Xunlei";
//假如选择是“是”
if (fbdDlg.ShowDialog() == DialogResult.OK)
{
textBox2.Text = fbdDlg.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
//判断是否输入
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请选择文件名、上传目录");
return;
}
Byte[] btInfo = null;
// 打开文件,读文件
if (File.Exists(textBox1.Text))
{
//将文件读成数据流的方式存在内存中
btInfo = File.ReadAllBytes(textBox1.Text);
}
//取得文件名
int lastNum = textBox1.Text.LastIndexOf('\\');
string fileName = textBox1.Text.Substring(lastNum + 1);
//写文件
if (!Directory.Exists(textBox2.Text))
{
Directory.CreateDirectory(textBox2.Text);
}
if (File.Exists(textBox2.Text + "\\" + fileName))
{
File.Delete(textBox2.Text + "\\" + fileName);
}
//向目标文件夹中写入文件
File.WriteAllBytes(textBox2.Text + "\\" + fileName, btInfo);
btInfo = null;
//弹出提示信息
MessageBox.Show("上传成功");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}