c#学习小笔记1:获取文件路径和文件夹路径

    xiaoxiao2024-12-21  15

     

    获取文件路径

    private void button1_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.InitialDirectory = "C:\\"; //打开对话框后的初始目录 fileDialog.Filter = "文本文件|*.bak|所有文件|*.*"; //定义文件允许类型 fileDialog.RestoreDirectory = false; //若为false,则打开对话框后为上次的目录。若为true,则为初始目录 if (fileDialog.ShowDialog() == DialogResult.OK) textBox1.Text = System.IO.Path.GetFullPath(fileDialog.FileName);//将选中的文件的路径传递给TextBox “FilePath” }

    获取文件夹路径

    private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog(); //设置根目录在桌面; myFolderBrowserDialog.RootFolder = System.Environment.SpecialFolder.Desktop; //设置当前选择的路径 myFolderBrowserDialog.SelectedPath = "C:"; //允许在对话框中包括一个新建目录的按钮 myFolderBrowserDialog.ShowNewFolderButton = true; //设置对话框的说明信息 myFolderBrowserDialog.Description = "请选择输出目录"; if (myFolderBrowserDialog.ShowDialog() == DialogResult.OK) {//确认是否保存 string strLuJing = myFolderBrowserDialog.SelectedPath;//获取路径 textBox1.Text = strLuJing;//赋值给文本显示 } }

     

    最新回复(0)