//打开
  private   void   open_file()   
  {   
  OpenFileDialog   op=new   OpenFileDialog();   
  op.RestoreDirectory=true;   
  if(DialogResult.OK==op.ShowDialog())   
      {//打开后事件}   
  }   
    
  //保存   
  private   void   save_file()   
  {   
  SaveFileDialog   sa=new   SaveFileDialog();   
  sa.RestoreDirectory=true;   
  if(DialogResult.OK==sa.ShowDialog())   
        {//保存后事件   
          //XX.Save(sa.Filename);}   
  }
弱弱的问下,打开和保存的事件具体怎么写?能给个范列??

解决方案 »

  1.   

     文件操作概览 
    趁有空,对.net的文件操作类做了一个大概的总结.首先引用IO namespace:
    using System.IO;创建目录:
            /// <summary>
            /// Directory.CreateDirectory参数解释:
            /// path
            /// 要创建的目录。
            /// directorySecurity
            /// 要应用于此目录的访问控制。
            /// 
            /// 如果文件夹存在则不创建,可用Directory.Exists判断文件夹是否存在.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    DirectoryInfo di = Directory.CreateDirectory(@"C:\FileOperator");
                    string message = string.Format("Create \"{0}\" success!", di.FullName);
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Create folder Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }创建文件:
            /// <summary>
            /// File.Create参数解释:
            /// path
            /// 类型:System..::.String
            /// 文件名。
            /// bufferSize
            /// 类型:System..::.Int32
            /// 用于读取和写入文件的已放入缓冲区的字节数。
            /// options
            /// 类型:System.IO..::.FileOptions
            /// FileOptions 值之一,它描述如何创建或覆盖该文件。
            /// fileSecurity
            /// 类型:System.Security.AccessControl..::.FileSecurity
            /// FileSecurity 值之一,它确定文件的访问控制和审核安全性。        /// 创建文件会覆盖原来同名文件,所以创建前最好File.Exists判断文件是否存在.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    FileStream fs = File.Create(@"C:\FileOperator\File1.txt");                string message = string.Format("Create \"{0}\" success!", fs.Name);
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    fs.Dispose();
                    fs.Close();
                }
                catch
                {
                    MessageBox.Show("Create file Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }打开和写入文件:
            /// <summary>
            /// File.Open参数解释:
            /// path
            /// 类型:System..::.String
            /// 要打开的文件。
            /// mode
            /// 类型:System.IO..::.FileMode
            /// FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。
            /// access
            /// 类型:System.IO..::.FileAccess
            /// FileAccess 值,指定可以对文件执行的操作。
            /// share
            /// 类型:System.IO..::.FileShare
            /// FileShare 值,它指定其他线程所具有的对该文件的访问类型。
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, EventArgs e)
            {
                try
                {
                    FileStream fs = File.Open(@"C:\FileOperator\File1.txt", FileMode.Append);
                    byte[] byteArray = new byte[] { (byte)'K', (byte)'E', (byte)'N' };
                    fs.Write(byteArray, 0, byteArray.Length);                string message = string.Format("Write \"KEN\" to File success!", fs.Name);
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    fs.Dispose();
                    fs.Close();
                }
                catch
                {
                    MessageBox.Show("Open & Write file Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }文件复制:
            /// <summary>
            /// File.Copy参数解释:
            /// sourceFileName: 源文件
            /// destFileName: 目标文件
            /// overwrite: 如果目标已存在,是否覆盖目标文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button4_Click(object sender, EventArgs e)
            {
                try
                {
                    DirectoryInfo di = Directory.CreateDirectory(@"C:\FileOperator\Temp");
                    File.Copy(@"C:\FileOperator\File1.txt", @"C:\FileOperator\Temp\File1.txt", true);
                    string message = string.Format("Copy File to \"{0}\" success!", @"C:\FileOperator\Temp\File1.txt");
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Copy file Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }文件删除:
            /// <summary>
            /// File.Delete参数解释:
            /// path: 文件完整路径;
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button5_Click(object sender, EventArgs e)
            {
                try
                {
                    File.Delete(@"C:\FileOperator\Temp\File1.txt");
                    string message = "Delete File success!";
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Delete file Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }移动和重命名:
            /// <summary>
            /// File.Move参数解释:
            /// sourceFileName: 源文件
            /// destFileName: 目标文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button6_Click(object sender, EventArgs e)
            {
                try
                {
                    //move
                    File.Move(@"C:\FileOperator\File1.txt", @"C:\FileOperator\Temp\File1.txt");
                    //rename
                    File.Move(@"C:\FileOperator\Temp\File1.txt", @"C:\FileOperator\Temp\File2.txt");                string message = "Move & Rename file success!";
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Move & Rename file Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }            //文件夹的Move和Rename同文件一样,也使用File.Move,传入文件路径即可.
            }
      

  2.   

    设置文件属性:
            /// <summary>
            /// File.SetAttributes参数解释:
            /// path: 文件完整路径;
            /// fileAttributes: FileAttributes值,指定文件属性.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button7_Click(object sender, EventArgs e)
            {
                try
                {
                    File.SetAttributes(@"C:\FileOperator\Temp\File2.txt", FileAttributes.ReadOnly | FileAttributes.Hidden);                string message = "Set file Readonly & Hidden success!";
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Set file attributes Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }检查文件和目录是否存在:
            /// <summary>
            /// 参数解释:
            /// path:文件路径
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button8_Click(object sender, EventArgs e)
            {
                try
                {
                    if (Directory.Exists(@"C:\FileOperator\Temp"))
                        MessageBox.Show(string.Format("\"{0}\"Exits!", @"C:\FileOperator\Temp\"), "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (File.Exists(@"C:\FileOperator\Temp\File2.txt"))
                        MessageBox.Show(string.Format("\"{0}\"Exits!", @"C:\FileOperator\Temp\File2.txt"), "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Check Folder & File Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }设置目录属性:
            /// <summary>
            /// 不用解释
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button9_Click(object sender, EventArgs e)
            {
                try
                {
                    DirectoryInfo di = new DirectoryInfo(@"C:\FileOperator\Temp");
                    di.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden;                string message = "Set folder attributes success!";
                    MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Set folder attributes Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }获取子目录:
             /// <summary>
            /// 懒得解释
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button10_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] children = Directory.GetDirectories(@"C:\FileOperator");
                    string message = string.Empty;
                    if (children.Length > 0)
                        message = string.Format("{0} folders in \"{1}\"", children.Length, @"C:\FileOperator");
                    else
                        message = string.Format("No folder in \"{0}\"", @"C:\FileOperator");                MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Get child folder Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }获取目录下所有文件:
            /// <summary>
            /// 呵呵
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button11_Click(object sender, EventArgs e)
            {
                try
                {
                    string[] files = Directory.GetFiles(@"C:\FileOperator");
                    string message = string.Empty;
                    if (files.Length > 0)
                        message = string.Format("{0} files in \"{1}\"", files.Length, @"C:\FileOperator");
                    else
                        message = string.Format("No file in \"{0}\"", @"C:\FileOperator");                MessageBox.Show(message, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Get files Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }删除目录:
            /// <summary>
            /// 参数解释:
            /// path:
            /// recursive:是否递归删除目录下所有目录及文件.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button12_Click(object sender, EventArgs e)
            {
                try
                {
                    //正常文件才可以删除
                    DirectoryInfo di = new DirectoryInfo(@"C:\FileOperator\Temp");
                    di.Attributes = FileAttributes.Normal;
                    File.SetAttributes(@"C:\FileOperator\Temp\File2.txt", FileAttributes.Normal);
                    //delete
                    Directory.Delete(@"C:\FileOperator", true);
                    MessageBox.Show("Delete folder success!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show("Delete folder Error!", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
     另外,.Net对txt文件提供了更友好的操作封装,具体请查看MSDN,path参数可以用绝对路径,也可以使用相对路径,个人推荐使用绝对路径.
      

  3.   

    补充一下问题:
    比如 我用OpenFileDialog 打开了一个文件
    如何获得这个文件的参数 以便我使用 SAVEFileDialog  保存一个副本
      

  4.   

    文件操作概览
    趁有空,对.net的文件操作类做了一个大概的总结
      

  5.   

    private void btnOpen_Click(object sender, EventArgs e)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.InitialDirectory = "c:\\";
                dlg.Filter = "文本文件|*.txt|RTF文件|*.RTF|所有文件|*.*";
                dlg.FilterIndex = 2;
                dlg.RestoreDirectory = true;
                if(dlg.ShowDialog() == DialogResult.OK)           
                {
                    try
                    {
                        string fName = dlg.FileName;
                        rtMain.LoadFile(fName);
                        printDocument.DocumentName = Path.GetFileName(fName);
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }            }
                rtMain.Focus();
            }
    这是我用openfiledialog做的
      

  6.   


    朋友 我重新描述一下我的问题,我想用OpenDialog打开一个文件,然后用ShowDialog把这个相应的文件另存一份,请问如何实现?关键是如何取得当前指定文件的 参数》》》
      

  7.   

    弱弱的问一下:
     private void button1_Click(object sender, EventArgs e)
            {
                myhome m = new myhome();            this.Hide();
                m.ShowDialog();}
         
    创建窗口句柄时出错 是怎么回事?怎么解决啊·?
      

  8.   

    问题 已经解决 是skinEngine出问题了
    输出 skinEngine 就运行正常了
      

  9.   

    private void btnOpen_Click(object sender, EventArgs e) 
            { 
                OpenFileDialog dlg = new OpenFileDialog(); 
                dlg.InitialDirectory = "c:\\"; 
                dlg.Filter = "文本文件|*.txt|RTF文件|*.RTF|所有文件|*.*"; 
                dlg.FilterIndex = 2; 
                dlg.RestoreDirectory = true; 
                if(dlg.ShowDialog() == DialogResult.OK)          
                { 
                    try 
                    { 
                        string fName = dlg.FileName; 
                        rtMain.LoadFile(fName); 
                        printDocument.DocumentName = Path.GetFileName(fName); 
                    } 
                    catch (System.IO.FileNotFoundException) 
                    { 
                        MessageBox.Show("没有找到文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                    }             } 
                rtMain.Focus(); 
            } 
    这是我用openfiledialog做的
      

  10.   

    请问  rtMain 是窗体的名字吗???
      

  11.   

    学习。
    .NET Framwork学得不好
    知道是用文件流
    但写不出代码。
    谢谢楼主提出来
    知道自己又要看看书了