[WPF] folder selection directory, file selection directory, save file directory

1. Main functions

        WPF provides these three functions. OpenFileDialog, SaveFileDialog, FolderBrowserDialog. When used, it can be written in the Click method of the button or in the bound Command. The essence of the three operations is to obtain the path name of the file.

二、FolderBrowserDialog

        open folder action

FolderBrowserDialog folderBrowserDialog = new FolderBrowserDIalog();
folderBrowserDialog.RootFolder="d:\\";    //设置初始目录
folderBrowserDialog.ShowDialog();        //这个方法可以显示文件夹选择对话框
string directoryPath=folderBrowserDialog.SelectedPath;    //获取选择的文件夹的全路径名

3. OpenFileDialog

        select file operation

OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.InitialDirectory="c:\\desktop";    //初始的文件夹
openFileDialog.Filter="Image1|*.bmp;*.jepg;*.png|Txt|*.txt|All|*.";//在对话框中显示的文件类型
openFileDialog.FilterIndex=2;
openFileDialog.RestoreDirectory=true;
openFileDialog.ShowDialog();    //显示对话框
string filepath=openFileDialog.FileName; //获取选择的文件的全路径名

4. SaveFileDialog

        The save file operation is actually a selection file operation. In short, it is a way to get the file path name.

SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Filter="Image1|*.bmp;*.png|Image2|*.jepg";
saveFileDialog.InitialDirectory = "C:\\Users\\Desktop\\Image"; //设置初始目录
if ((bool)saveFileDialog.ShowDialog() && Image=null)
{
    string name=saveFileDialog.FileName; //获取选择的文件,或者自定义的文件名的全路径。
    Image.ImWrite(name);    //将文件进行保存,这里IO流或者其它保存文件方法都可以
}

Guess you like

Origin blog.csdn.net/weixin_43163656/article/details/127996019