Skip to content

Commit ff9d91a

Browse files
committed
v4.0.0 C#重写
1 parent 0e1eb90 commit ff9d91a

26 files changed

+2598
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
obj/
2+
.vs/

App.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
5+
</startup>
6+
<appSettings>
7+
<add key="AutoCheckUpdate" value="true"/>
8+
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true"/>
9+
</appSettings>
10+
</configuration>

AppConfig.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.IO;
2+
using System.Text;
3+
using System.Configuration;
4+
5+
namespace AppConfig_cs
6+
{
7+
public class AppConfig
8+
{
9+
public static string GetValue(string Key, string Default, string ConfigPath) // 读取程序自身配置文件,返回:值,参数:项、默认值、配置文件路径
10+
{
11+
if (!File.Exists(ConfigPath))
12+
{
13+
File.WriteAllText(ConfigPath, 磁贴美化小工具.Properties.Resources.AppConfig, Encoding.UTF8);
14+
}
15+
Configuration App_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
16+
if (App_Config.AppSettings.Settings[Key] == null || App_Config.AppSettings.Settings[Key].Value == null)
17+
{
18+
return Default;
19+
}
20+
return App_Config.AppSettings.Settings[Key].Value;
21+
}
22+
23+
public static void SetValue(string Key, string Value, string ConfigPath) // 写出程序自身配置文件,无返回,参数:项、值、配置文件路径
24+
{
25+
if (!File.Exists(ConfigPath))
26+
{
27+
File.WriteAllText(ConfigPath, 磁贴美化小工具.Properties.Resources.AppConfig, Encoding.UTF8);
28+
}
29+
Configuration App_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
30+
if(App_Config.AppSettings.Settings[Key] == null)
31+
{
32+
App_Config.AppSettings.Settings.Add(Key, Value);
33+
}
34+
else
35+
{
36+
App_Config.AppSettings.Settings[Key].Value = Value;
37+
}
38+
App_Config.Save(ConfigurationSaveMode.Modified);
39+
ConfigurationManager.RefreshSection("appSettings");
40+
}
41+
}
42+
}

File.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using IWshRuntimeLibrary;
2+
using System.Diagnostics;
3+
4+
namespace File_cs
5+
{
6+
public class Shortcut
7+
{
8+
public static string Get_Shortcut_TargetPath(string Shortcut_Path) // 获取快捷方式目标路径,返回:目标路径,参数:快捷方式路径
9+
{
10+
if (System.IO.File.Exists(Shortcut_Path))
11+
{
12+
WshShell Shell = new WshShell();
13+
IWshShortcut Shortcut = (IWshShortcut)Shell.CreateShortcut(Shortcut_Path);
14+
return Shortcut.TargetPath;
15+
}
16+
return "";
17+
}
18+
19+
public static string Get_Shortcut_TargetPath_Array(string[] Shortcut_Path, string Path) // 从数组中匹配快捷方式目标路径,返回:匹配目标的快捷方式路径,参数:快捷方式数组(字符串)、目标路径
20+
{
21+
if (Shortcut_Path.Length > 0)
22+
{
23+
foreach (string Temp_Shortcut_Path in Shortcut_Path)
24+
{
25+
if (Get_Shortcut_TargetPath(Temp_Shortcut_Path) == Path)
26+
{
27+
return Temp_Shortcut_Path;
28+
}
29+
}
30+
}
31+
return "";
32+
}
33+
public static void Create_Shortcut(string LinkPath, string TargetPath, string IconPath = "")
34+
{
35+
WshShell shell = new WshShell();
36+
IWshShortcut Shortcut = (IWshShortcut)shell.CreateShortcut(LinkPath);
37+
Shortcut.TargetPath = TargetPath;
38+
if (IconPath != "")
39+
{
40+
Shortcut.IconLocation = IconPath;
41+
}
42+
Shortcut.Save();
43+
}
44+
}
45+
public class File
46+
{
47+
public static string[] File_Enumeration(string Path, string FileName, bool Traversal) // 枚举文件,返回:文件数组(字符串),参数:欲寻找的路径、欲寻找的文件名(允许通配符 *.lnk)、是否遍历子目录
48+
{
49+
if (Path.Substring(Path.Length - 1, 1) != @"\")
50+
{
51+
Path += @"\";
52+
}
53+
if (Traversal == true)
54+
{
55+
return System.IO.Directory.GetFiles(Path, FileName, System.IO.SearchOption.AllDirectories);
56+
}
57+
else
58+
{
59+
return System.IO.Directory.GetFiles(Path, FileName, System.IO.SearchOption.TopDirectoryOnly);
60+
}
61+
}
62+
}
63+
}

FileDropAdmin.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
using System.Windows.Forms;
6+
7+
namespace FileDropAdmin_cs
8+
{
9+
// 解决管理员权限下无法拖放文件的问题
10+
public sealed class FileDropAdmin : IMessageFilter, IDisposable
11+
{
12+
13+
#region native members
14+
15+
[DllImport("user32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
16+
[return: MarshalAs(UnmanagedType.Bool)]
17+
private static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint message, ChangeFilterAction action, in ChangeFilterStruct pChangeFilterStruct);
18+
19+
[DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)]
20+
private static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept);
21+
22+
[DllImport("shell32.dll", SetLastError = false, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
23+
private static extern uint DragQueryFile(IntPtr hWnd, uint iFile, StringBuilder lpszFile, int cch);
24+
25+
[DllImport("shell32.dll", SetLastError = false, CallingConvention = CallingConvention.Winapi)]
26+
private static extern void DragFinish(IntPtr hDrop);
27+
28+
[StructLayout(LayoutKind.Sequential)]
29+
private struct ChangeFilterStruct
30+
{
31+
public uint CbSize;
32+
public ChangeFilterStatu ExtStatus;
33+
}
34+
35+
private enum ChangeFilterAction : uint
36+
{
37+
MSGFLT_RESET,
38+
MSGFLT_ALLOW,
39+
MSGFLT_DISALLOW
40+
}
41+
42+
private enum ChangeFilterStatu : uint
43+
{
44+
MSGFLTINFO_NONE,
45+
MSGFLTINFO_ALREADYALLOWED_FORWND,
46+
MSGFLTINFO_ALREADYDISALLOWED_FORWND,
47+
MSGFLTINFO_ALLOWED_HIGHER
48+
}
49+
50+
private const uint WM_COPYGLOBALDATA = 0x0049;
51+
private const uint WM_COPYDATA = 0x004A;
52+
private const uint WM_DROPFILES = 0x0233;
53+
54+
#endregion
55+
56+
57+
private const uint GetIndexCount = 0xFFFFFFFFU;
58+
59+
private Control _ContainerControl;
60+
61+
private readonly bool _DisposeControl;
62+
63+
public Control ContainerControl { get; }
64+
65+
public FileDropAdmin(Control containerControl) : this(containerControl, false) { }
66+
67+
public FileDropAdmin(Control containerControl, bool releaseControl)
68+
{
69+
_ContainerControl = containerControl ?? throw new ArgumentNullException("control", "control is null.");
70+
71+
if (containerControl.IsDisposed) throw new ObjectDisposedException("control");
72+
73+
_DisposeControl = releaseControl;
74+
75+
var status = new ChangeFilterStruct() { CbSize = 8 };
76+
77+
if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_DROPFILES, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
78+
if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYGLOBALDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
79+
if (!ChangeWindowMessageFilterEx(containerControl.Handle, WM_COPYDATA, ChangeFilterAction.MSGFLT_ALLOW, in status)) throw new Win32Exception(Marshal.GetLastWin32Error());
80+
DragAcceptFiles(containerControl.Handle, true);
81+
82+
Application.AddMessageFilter(this);
83+
}
84+
85+
public bool PreFilterMessage(ref Message m)
86+
{
87+
if (_ContainerControl == null || _ContainerControl.IsDisposed) return false;
88+
if (_ContainerControl.AllowDrop) return _ContainerControl.AllowDrop = false;
89+
if (m.Msg == WM_DROPFILES)
90+
{
91+
var handle = m.WParam;
92+
93+
var fileCount = DragQueryFile(handle, GetIndexCount, null, 0);
94+
//Debug.Print(fileCount.ToString());
95+
var fileNames = new string[fileCount];
96+
//Debug.Print(fileNames[1]);
97+
var sb = new StringBuilder(262);
98+
var charLength = sb.Capacity;
99+
for (uint i = 0; i < fileCount; i++)
100+
{
101+
if (DragQueryFile(handle, i, sb, charLength) > 0) fileNames[i] = sb.ToString();
102+
//Debug.Print(fileNames[i]);
103+
}
104+
DragFinish(handle);
105+
_ContainerControl.AllowDrop = true;
106+
_ContainerControl.DoDragDrop(fileNames, DragDropEffects.All);
107+
_ContainerControl.AllowDrop = false;
108+
return true;
109+
}
110+
return false;
111+
}
112+
113+
public void Dispose()
114+
{
115+
if (_ContainerControl == null)
116+
{
117+
if (_DisposeControl && !_ContainerControl.IsDisposed) _ContainerControl.Dispose();
118+
Application.RemoveMessageFilter(this);
119+
_ContainerControl = null;
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)