用C#開啟檔案對話方塊的方法和簡單使用的程式

來源:互聯網
上載者:User

標籤:

上面是我建立的Winform表單,拉控制項就這樣,groupbox顯示檔案的相關資訊

代碼如下:

 
        //執行個體化檔案對話方塊的類對象
        OpenFileDialog ofg = new OpenFileDialog();
        //存放源檔案路徑,一個存放目的檔案路徑
        string filepath;
        string filepath1;
        //擷取檔案對象
        FileInfo file = null;
        private void button1_Click(object sender, EventArgs e)
        {           
            //調用對象的函數
            if (ofg.ShowDialog() == DialogResult.OK) {
                //擷取檔案的路徑
                filepath = ofg.FileName;
                this.txb_Path1.Text = filepath;
                //建立檔案對象
                file = new FileInfo(filepath);
                //擷取檔案名稱
                this.txtFileName.Text = file.Name;
                //擷取檔案完整路徑
                this.txtFilePathAll.Text = file.FullName;
                //擷取檔案所在的根目錄
                this.txtFileRoot.Text = file.DirectoryName;
                this.txtCreatetime.Text = file.CreationTime.ToString();
                this.txtVisttime.Text = file.LastAccessTime.ToString();
                this.txtUpdatetime.Text = file.LastWriteTime.ToString();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //擷取檔案目錄
            FolderBrowserDialog fog = new FolderBrowserDialog();
            if (fog.ShowDialog() == DialogResult.OK)
            {
                //擷取存放目錄(檔案夾)
                filepath1 = fog.SelectedPath;
                this.txb_Path2.Text = filepath1;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            filepath1 = [email protected]"\" + file.Name;
            File.Copy(filepath,filepath1);
            MessageBox.Show("複製成功!");

        }

運行效果如下:

 

用C#開啟檔案對話方塊的方法和簡單使用的程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.