现在我想把一个txt文件复制,在复制之前让用户选择保存位置,这个用c#语言怎么实习实现?

解决方案 »

  1.   

    不是有savedialog对话框组件吗,然后用 System.IO.File.Copy 复制文件
      

  2.   

    让用户选择保存的文件用SaveFileDialog:
    http://msdn.microsoft.com/zh-cn/library/microsoft.win32.savefiledialog.aspx复制,可以读取再保存
    File.ReadAllText(String)
    File.WriteAllText(String, String)
    也可以拷贝
    File.Copy(String, String)参考:
    File Methods
    http://msdn.microsoft.com/en-us/library/3z2ck8eh.aspx
      

  3.   

    using System;
    using System.IO;
    using System.Windows.Forms;namespace CopyFile
    {
        public partial class FormMain : Form
        {
            #region 常量        private const string Filter      = "文本文件(.txt)|*.txt";
            private const string TitleSource = "请选择要复制的文件";
            private const string TitleDest   = "请设置要复制到的文件";        #endregion        #region 构造函数        public FormMain()
            {
                InitializeComponent();
            }        #endregion        #region 控件事件        private void ButDoCopy_Click(object sender, EventArgs e)
            {
                var ofd = new OpenFileDialog { Filter = Filter, Title = TitleSource };
                if (ofd.ShowDialog() != DialogResult.OK) return;
                var sfd = new SaveFileDialog { Filter = Filter, Title = TitleDest };
                if (sfd.ShowDialog() != DialogResult.OK) return;
                try
                {
                    File.Copy(ofd.FileName, sfd.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        #endregion
        }
    }