WPF 选择文件选择文件夹

namespace Microsoft.Win32

 

选择文件:

if (string.IsNullOrEmpty(folderInitialDirectory))
{
folderInitialDirectory = FileAndFolderOperation.Instance.GetUserDocumentFolder();
}

SaveFileDialog saveFileDialog = new SaveFileDialog();


saveFileDialog.Title = Resources.ChooseExportFilePathWndTitle;
saveFileDialog.InitialDirectory = folderInitialDirectory;
saveFileDialog.Filter = Resources.ChooseExportFilePathWndFilter;
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == true)
{

FilesPath = saveFileDialog.FileName;
folderInitialDirectory = System.IO.Path.GetDirectoryName(FilesPath);
}

 

选择文件夹:

Microsoft.WindowsAPICodePack.Dialogs

CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;  // 这里一定要设置true,不然就是选择文件
if (string.IsNullOrEmpty(folderInitialDirectory))
{
folderInitialDirectory = FileAndFolderOperation.Instance.GetUserDocumentFolder();
}
dialog.InitialDirectory = folderInitialDirectory;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ToFolderPath = dialog.FileName;
ImportExtentions.Instance.SetImpToFolderName(ToFolderPath);
folderInitialDirectory =ToFolderPath;
var zippath = ImportExtentions.Instance.ImpFromFolderName;
var strZipPath = System.IO.Path.GetDirectoryName(zippath);
string strRoot = System.IO.Path.GetPathRoot(strZipPath);
var zipFreeSpace = FileAndFolderOperation.Instance.GetHardDiskFreeSpace(strRoot);

string strRoot1 = "";
var strPath = System.IO.Path.GetDirectoryName(ToFolderPath);
if(!string.IsNullOrWhiteSpace(strPath))
strRoot1 = System.IO.Path.GetPathRoot(strPath);
else
strRoot1 = System.IO.Path.GetPathRoot(ToFolderPath);

var lfreeSpace = FileAndFolderOperation.Instance.GetHardDiskFreeSpace(strRoot1);

if (zipFreeSpace > lfreeSpace)
{
SetSizeErrorInfo();
return;
}}

上一篇:c# – 如何使用SaveFileDialog的过滤器


下一篇:Redis GEO ,GEOHASH,Spatial_index