1. 程式人生 > >WinForm中SaveFileDialog的用法

WinForm中SaveFileDialog的用法

#region 儲存對話方塊
        private void ShowSaveFileDialog()
        {
            //string localFilePath, fileNameExt, newFileName, FilePath;  
            SaveFileDialog sfd = new SaveFileDialog();

            //設定檔案型別  
            sfd.Filter = "資料庫備份檔案(*.bak)|*.bak|資料檔案(*.mdf)|*.mdf|日誌檔案(*.ldf)|*.ldf";

            //設定預設檔案型別顯示順序  
            sfd.FilterIndex = 1;

            //儲存對話方塊是否記憶上次開啟的目錄  
            sfd.RestoreDirectory = true;

            //點了儲存按鈕進入  
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = sfd.FileName.ToString();     //獲得檔案路徑
                string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("//") + 1);     //獲取檔名,不帶路徑

                //獲取檔案路徑,不帶檔名  
                //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("//"));  

                //給檔名前加上時間  
                //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;  

                //在檔名里加字元  
                //saveFileDialog1.FileName.Insert(1,"dameng");  

                //System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//輸出檔案  

                ////fs輸出帶文字或圖片的檔案,就看需求了  
            }
        }

        #endregion