guanqq
寻找沙漠绿洲

导航

统计
公告
 

2008年6月13日

首先,读取目标文件
 1        private void button1_Click(object sender, EventArgs e)
 2        {
 3
 4            //建立一个打开文件对象
 5            OpenFileDialog ofdAim = new OpenFileDialog();
 6
 7            //初始首选路径
 8            ofdAim.InitialDirectory = "c:\\";
 9
10            //扩展名
11            ofdAim.Filter = "txt files (*.txt) |*.txt|All files (*.*)|*.*";
12
13            //如果选择了是,应该执行什么操作
14            if (ofdAim.ShowDialog() == DialogResult.OK)
15            {
16                textBox1.Text = ofdAim.FileName;
17            }

18        }

19

其次,选择源文件存储的路径
 1      private void button2_Click(object sender, EventArgs e)
 2        {
 3            //选择当前的文件夹对象
 4            FolderBrowserDialog fbdDlg = new FolderBrowserDialog();
 5            fbdDlg.SelectedPath = "F:\\Xunlei";
 6            
 7            //假如选择是“是”
 8            if (fbdDlg.ShowDialog() == DialogResult.OK)
 9            {
10                textBox2.Text = fbdDlg.SelectedPath;
11            }

12        }

13
最后,点了存储按钮,保存
 1        private void button3_Click(object sender, EventArgs e)
 2        {
 3            try
 4            {
 5                //判断是否输入
 6                if (textBox1.Text == "" || textBox2.Text == "")
 7                {
 8                    MessageBox.Show("请选择文件名、上传目录");
 9                    return;
10                }

11
12                Byte[] btInfo = null;
13                // 打开文件,读文件
14                if (File.Exists(textBox1.Text))
15                {
16                    //将文件读成数据流的方式存在内存中
17                    btInfo = File.ReadAllBytes(textBox1.Text);
18                }

19
20                //取得文件名
21                int lastNum = textBox1.Text.LastIndexOf('\\');
22                string fileName = textBox1.Text.Substring(lastNum + 1);
23               
24                //写文件
25                if (!Directory.Exists(textBox2.Text))
26                {
27                    Directory.CreateDirectory(textBox2.Text);
28                }

29
30                if (File.Exists(textBox2.Text + "\\" + fileName))
31                {
32                    File.Delete(textBox2.Text + "\\" + fileName);
33                }

34
35                //向目标文件夹中写入文件
36                File.WriteAllBytes(textBox2.Text + "\\" + fileName, btInfo);
37                btInfo = null;
38
39                //弹出提示信息
40                MessageBox.Show("上传成功");
41            }

42            catch(Exception ex)
43            {
44                MessageBox.Show(ex.Message);
45            }

46
47        }
posted @ 2008-06-13 11:21 沙漠之驼 阅读(68) 评论(0) 编辑