資料夾操作
窗體程式,開啟資料夾選擇對話方塊,單選和多選
這不是一條刪除線
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FolderDialog
{
public class FolderSelectDialog
{
System.Windows.Forms.OpenFileDialog ofd = null;
public FolderSelectDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "Folders|\n";
ofd.AddExtension = false;
ofd.CheckFileExists = false;
ofd.DereferenceLinks = true;
ofd.Multiselect = Multiselect;
}
public string InitialDirectory
{
get { return ofd.InitialDirectory; }
set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
}
public string Title
{
get { return ofd.Title; }
set { ofd.Title = value == null ? "Select a folder" : value; }
}
public bool Multiselect
{
get { return ofd.Multiselect; }
set { ofd.Multiselect = value == false ? false : value; }
}
public string FileName
{
get { return ofd.FileName; }
}
public string[] FileNames
{
get { return ofd.FileNames; }
}
public bool ShowDialog()
{
return ShowDialog(IntPtr.Zero);
}
public bool ShowDialog(IntPtr hWndOwner)
{
bool flag = false;
if (Environment.OSVersion.Version.Major >= 6)
{
var r = new Reflector("System.Windows.Forms");
uint num = 0;
Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
object dialog = r.Call(ofd, "CreateVistaDialog");
r.Call(ofd, "OnBeforeVistaDialog", dialog);
uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
object[] parameters = new object[] { pfde, num };
r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
num = (uint)parameters[1];
try
{
int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
flag = 0 == num2;
}
finally
{
r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
GC.KeepAlive(pfde);
}
}
else
{
var fbd = new FolderBrowserDialog();
fbd.Description = this.Title;
fbd.SelectedPath = this.InitialDirectory;
fbd.ShowNewFolderButton = false;
if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
ofd.FileName = fbd.SelectedPath;
flag = true;
}
return flag;
}
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
public class Reflector
{
string m_ns;
Assembly m_asmb;
public Reflector(string ns)
: this(ns, ns)
{ }
public Reflector(string an, string ns)
{
m_ns = ns;
m_asmb = null;
foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
{
if (aN.FullName.StartsWith(an))
{
m_asmb = Assembly.Load(aN);
break;
}
}
}
public Type GetType(string typeName)
{
Type type = null;
string[] names = typeName.Split('.');
if (names.Length > 0)
type = m_asmb.GetType(m_ns + "." + names[0]);
for (int i = 1; i < names.Length; ++i)
{
type = type.GetNestedType(names[i], BindingFlags.NonPublic);
}
return type;
}
public object New(string name, params object[] parameters)
{
Type type = GetType(name);
ConstructorInfo[] ctorInfos = type.GetConstructors();
foreach (ConstructorInfo ci in ctorInfos)
{
try
{
return ci.Invoke(parameters);
}
catch { }
}
return null;
}
public object Call(object obj, string func, params object[] parameters)
{
return Call2(obj, func, parameters);
}
public object Call2(object obj, string func, object[] parameters)
{
return CallAs2(obj.GetType(), obj, func, parameters);
}
public object CallAs(Type type, object obj, string func, params object[] parameters)
{
return CallAs2(type, obj, func, parameters);
}
public object CallAs2(Type type, object obj, string func, object[] parameters)
{
MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return methInfo.Invoke(obj, parameters);
}
public object Get(object obj, string prop)
{
return GetAs(obj.GetType(), obj, prop);
}
public object GetAs(Type type, object obj, string prop)
{
PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return propInfo.GetValue(obj, null);
}
public object GetEnum(string typeName, string name)
{
Type type = GetType(typeName);
FieldInfo fieldInfo = type.GetField(name);
return fieldInfo.GetValue(null);
}
}
}
public class MultiSelect
{
public static string OpenFileBrowserDialog(bool multiselect)
{
FolderSelectDialog fbd = new FolderSelectDialog();
fbd.Multiselect = multiselect;
fbd.ShowDialog();
string selected_folders = "";
if (multiselect)
{
string[] names = fbd.FileNames;
selected_folders = string.Join(",", names);
}
else
{
selected_folders = fbd.FileName;
}
return selected_folders;
}
public static string[] OpenFileBrowserDialogs()
{
FolderSelectDialog fbd = new FolderSelectDialog();
fbd.Multiselect = true;
fbd.ShowDialog();
return fbd.FileNames;
}
public static void Form1_Load()
{
string name = OpenFileBrowserDialog(true);
MessageBox.Show(name);
Environment.Exit(0);
}
}
}
相關文章
- 資料夾操作庫os.path
- 大量資料夾批次重新命名的操作
- SharpZipLib解壓資料夾 包含空資料夾
- .git資料夾Git
- Android中asset資料夾和raw資料夾區別Android
- Qt 選擇資料夾、建立資料夾以及建立檔案QT
- ftp複製檔案或資料夾時出錯,操作超時FTP
- mkdir() 建立資料夾
- windows資料夾大小Windows
- java建立資料夾Java
- laravel 建立資料夾Laravel
- win10使用者資料夾遷移操作方法 win10怎麼移動使用者資料夾位置Win10
- linux刪除資料夾下所有檔案命令是什麼 linux刪除資料夾下內所有內容怎麼操作Linux
- DeerOJ的前端框架介紹-libs資料夾和controller資料夾前端框架Controller
- node_modules 資料夾下 .bin 隱藏資料夾的作用
- documents是什麼資料夾 documents資料夾可以刪除嗎
- drivers是什麼資料夾 drivers資料夾可以刪除嗎
- temp資料夾可以刪除嗎 temp資料夾幹啥的
- appdata資料夾在哪裡_win10找不到appdata資料夾APPWin10
- 資料夾變exe資料找回方法
- 使用JSZip實現在瀏覽器中操作檔案與資料夾JS瀏覽器
- win10 office2010關閉共享資料夾同步怎麼操作Win10
- .gitignore 在已忽略資料夾中不忽略指定檔案、資料夾...Git
- 畸形檔案 資料夾
- CoLab刪除資料夾
- 如何批次新建資料夾?
- 資料夾橫向排版
- 使用C#選擇資料夾、開啟資料夾、選擇檔案C#
- python 如何刪除資料夾下的所有檔案和子資料夾?Python
- ***python看圖軟體***(+-切換資料夾,d刪除所在資料夾)Python
- Mac怎麼給資料夾設定密碼?mac資料夾加密教程Mac密碼加密
- 資料夾裡的檔案怎麼設定跟外資料夾同名
- Win10的appdata資料夾在哪裡 電腦appdata資料夾在哪Win10APP
- 資料夾能直接設定密碼嗎 資料夾加密的常用方法密碼加密
- 批量新建資料夾並命名的辦法 如何批量新建很多資料夾
- 批次新建資料夾並命名的辦法 如何批次新建很多資料夾
- 把多個資料夾中的檔案批量放到一個資料夾
- win10我的電腦6個資料夾如何清理_win10刪除我的電腦6個資料夾操作步驟Win10