winform中 saveFileDialog 和openFileDialog 保存文件的用法


saveFileDialog  用法 

#region saveFileDialog的實現
            {
                //string localFilePath, fileNameExt, newFileName, FilePath;  
                string localFilePath = String.Empty;
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                //設置文件類型  
                saveFileDialog1.Filter = " xls files(*.xls)|*.txt|All files(*.*)|*.*";
                //設置文件名稱:
                saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd") + "-" + "資產信息報表.xls";

                //設置默認文件類型顯示順序  
                saveFileDialog1.FilterIndex = 2;

                //保存對話框是否記憶上次打開的目錄  
                saveFileDialog1.RestoreDirectory = true;

                //點了保存按鈕進入  
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //獲得文件路徑  
                    localFilePath = saveFileDialog1.FileName.ToString();
                    //string filname = this.openFileDialog2.FileName;
                    //獲取文件名,不帶路徑  
                    //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)saveFileDialog1.OpenFile();//輸出文件  

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

openFileDialog 用法:

 #region openFileDialog保存的用法
            {
                //string filname = this.openFileDialog2.FileName;
                //// 設置根在桌面SpecialFolder
                //fbdSelectFolder.RootFolder = Environment.SpecialFolder.Desktop;

                //// 設置當前選擇的路徑
                //fbdSelectFolder.SelectedPath = "C:\\";

                //// 允許在對話框中包括一個新建目錄的按鈕
                //fbdSelectFolder.ShowNewFolderButton = true;

                //// 設置對話框的說明信息
                //fbdSelectFolder.Description = "請選擇輸出目錄";

                //string selectPath = "資產信息報表";

                //if (fbdSelectFolder.ShowDialog() == DialogResult.OK)
                //{
                //    // 在此添加代碼
                //    selectPath = fbdSelectFolder.SelectedPath;
                //}
            }
            #endregion


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM