[C#][原创]打开文件夹和打开文件和保存文件

 选择文件操作代码:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "文本文件(*.txt)|*.txt|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件(*.*)|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect=false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//操作代码
}
选择文件夹代码:
System.Windows.Forms.FolderBrowserDialog dialog =new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "请选择Txt所在文件夹";
if (dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK  )
{
//操作代码

}
保存文件:
   private void ShowSaveFileDialog()
   {
         SaveFileDialog sfd = new SaveFileDialog(); //设置文件类型 
          sfd.Filter = "文本文件(*.txt)|*.txt";
         sfd.FilterIndex = 1;//设置默认文件类型显示顺序
         sfd.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录 
        sfd.FileName = "YourFileName";//设置默认的文件名
         if (sfd.ShowDialog() == DialogResult.OK) //点了保存按钮进入 
        {
             //操作代码

         }
   }

猜你喜欢

转载自blog.csdn.net/FL1623863129/article/details/113529776