【WPF】Toolkit(一个项目)的要点总结

【WPF】Toolkit(一个项目)的要点总结架构相关1.插件式开发:MEF具体怎么使用可参考百度+Demo(密码:k8ck)2.备份机制(项目特有功能)待续3.镜像机制(项目特有功能)待续4.分模块记录日志(转)非常完善的Log4net详细说明UI相关1.多语言读取系统的显示语言(displayLanguage),显示语言的定义是:假如你的系统现在是中文的,

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

架构相关

1. 插件式开发:MEF

具体怎么使用可参考百度+Demo (密码: k8ck)

2. 备份机制(项目独有功能)

3. 镜像机制(项目独有功能)

4. 分模块记录日志

(转)非常完善的Log4net详细说明

UI相关

1. 多语言

读取系统的显示语言(displayLanguage),显示语言的定义是:假如你的系统现在是中文的,你把它切换到了英文,但是英文的语言包并没有下载下来或者并没有将英文设置为显示语言,那么注销系统再登录之后,你系统显示的将还是中文。此时中文就是显示语言。

  • 显示语言的获取
    通过System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName可以拿到两个字符的ISO语言名称,如:zh,nl,en,fr,ja等。通过System.Globalization.CultureInfo.CurrentUICulture.Name获取区域名称,如:zh-CN,zh-SG,zh-TW,nl-NL,en-US,fr-FR,ja-JP等。第二个是第一个的子类。
  • 处理翻译好的多语言文件
    每种翻译好的语言都是一个ResourceDictionary的文件,对于同一个字符串这N个文件都用同一个key,只是value是不同的翻译。这里以英语的ResourceDictionary文件为基准(称为file1),读取当前显示语言对应的ResourceDictionary(称为file2)。将file2中每个key的value,覆盖file1中对应key的value上。这样如果file2中有哪些字符串没来得及翻译,在程序中将以英语的形式展示。
  • 将ResourceDictionary添加的主程序的Resource中
    this.Resources.MergedDictionaries.Add(languageResource);
2. 鼠标位置捕获

可参考:【WPF】DPI对控件定位产生的影响

3. 在win10弹出toast

可参考:【WPF】右下角弹出自定义通知样式(Notification)——简单教程

4. 自定义日历控件

可参考:【C#】wpf自定义calendar日期选择控件的样式

5. 高对比度主题处理
  • 如果.Net Framework>=4.5
    可订阅SystemParameters.StaticPropertyChanged事件,然后在订阅方法里写:
 private void SystemParameters_StaticPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        { 
   
            if (e.PropertyName == "HighContrast" && SystemParameters.HighContrast)
            { 
   
                var item = SystemParameters.HighContrastKey;

                if (SystemColors.WindowBrush.Color == Colors.White)
                { 
   
                    //高对比度白色
                    ell.Fill = new SolidColorBrush(Colors.Black);
                }
                else
                { 
   
                    //高对比黑色
                    ell.Fill = new SolidColorBrush(Colors.White);
                }
            }
        }
  • 如果.Net Framework<4.5
    可订阅SystemEvents.UserPreferenceChanged,然后写:
 private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        { 
   

            if(SystemParameters.HighContrast)
            { 
   
                if (SystemColors.WindowBrush.Color == Colors.White)
                { 
   
                    ell.Fill = new SolidColorBrush(Colors.Black);
                }
                else
                { 
   
                    ell.Fill = new SolidColorBrush(Colors.White);
                }
            }
        }
6. gif动图的显示

可参考:【C#】wpf添加gif动图支持

文件设备相关

1. 通过shell处理系统的特殊目录,读取相关属性

可参考:【C#】WindowsAPICodePack-Shell使用教程

2. 读取文件和文件夹的缩略图

可参考: 【C#】获取任意文件的缩略图

3. 长路径管理(解决PathTooLong的异常)

可参考:【C#】简单解决PathTooLong的Exception

4. 磁盘的格式化
  • 调用系统的格式化框:
        [DllImport("shell32.dll")]
        public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint option);

使用时:

                    int index = driverletter - 'a';//driverletter是:c,d,e,f等
                    SHFormatDrive(_pageHandle, Convert.ToUInt32(index), 0xFFFF, 0x0001);
  • 自动格式化为NTFS(一个帮助类)
    public class DriveManager
    { 
   
        #region SetLabel

        /// <summary>
        /// set a drive label to the desired value
        /// </summary>
        /// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
        /// <param name="label">label for the drive</param>
        /// <returns>true if success, false if failure</returns>
        public static bool SetLabel(char driveLetter, string label = "")
        { 
   
            #region args check

            if (!Char.IsLetter(driveLetter))
            { 
   
                return false;
            }
            if (label == null)
            { 
   
                label = "";
            }

            #endregion
            try
            { 
   
                DriveInfo di = DriveInfo.GetDrives()
                                        .Where(d => d.Name.StartsWith(driveLetter.ToString()))
                                        .FirstOrDefault();
                di.VolumeLabel = label;
                return true;
            }
            catch (Exception)
            { 
   
                return false;
            }
        }

        #endregion

        #region FormatDrive

        /// <summary>
        /// Format a drive using the best available method
        /// </summary>
        /// <param name="driveLetter">drive letter. Example : 'A:', 'B:', 'C:', 'D:', ..., 'Z:'.</param>
        /// <param name="label">label for the drive</param>
        /// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
        /// <param name="quickFormat">quick formatting?</param>
        /// <param name="enableCompression">enable drive compression?</param>
        /// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
        /// <returns>true if success, false if failure</returns>
        public static bool FormatDrive(string driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
        { 
   
            if (string.IsNullOrEmpty(driveLetter) || driveLetter.Length != 2) return false;
            return FormatDrive_CommandLine(driveLetter, label, fileSystem, quickFormat, enableCompression, clusterSize);
        }

        #endregion

        #region FormatDrive_CommandLine

        /// <summary>
        /// Format a drive using Format.com windows file
        /// </summary>
        /// <param name="driveLetter">drive letter. Example : 'A:', 'B:', 'C:', 'D:', ..., 'Z:'.</param>
        /// <param name="label">label for the drive</param>
        /// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
        /// <param name="quickFormat">quick formatting?</param>
        /// <param name="enableCompression">enable drive compression?</param>
        /// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
        /// <returns>true if success, false if failure</returns>
        public static bool FormatDrive_CommandLine(string driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
        { 
   
            #region args check

            if (driveLetter.Length!=2 ||
                !IsFileSystemValid(fileSystem))
            { 
   
                return false;
            }

            #endregion
            bool success = false;
            try
            { 
   
                var di = new DriveInfo(driveLetter);
                var psi = new ProcessStartInfo();
                psi.FileName = "format.com";
                psi.WorkingDirectory = Environment.SystemDirectory;
                psi.Arguments = "/FS:" + fileSystem +
                                             " /Y" +
                                             " /V:" + label +
                                             (quickFormat ? " /Q" : "") +
                                             ((fileSystem == "NTFS" && enableCompression) ? " /C" : "") +
                                             (clusterSize.HasValue ? " /A:" + clusterSize.Value : "") +
                                             " " + driveLetter;
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardInput = true;
                var formatProcess = Process.Start(psi);
                var swStandardInput = formatProcess.StandardInput;
                swStandardInput.WriteLine();
                formatProcess.WaitForExit();
                success = true;
            }
            catch (Exception) { 
    }
            return success;
        }

        #endregion

        #region FormatDrive_Shell32

        #region interop

        // http://msdn.microsoft.com/en-us/library/windows/desktop/bb762169(v=vs.85).aspx
        [DllImport("shell32.dll")]
        private static extern uint SHFormatDrive(IntPtr hwnd, uint drive, SHFormatFlags fmtID, SHFormatOptions options);

        private enum SHFormatFlags : uint
        { 
   
            SHFMT_ID_DEFAULT = 0xFFFF,
            /// <summary>
            /// A general error occured while formatting. This is not an indication that the drive cannot be formatted though.
            /// </summary>
            SHFMT_ERROR = 0xFFFFFFFF,
            /// <summary>
            /// The drive format was cancelled by user/OS.
            /// </summary>
            SHFMT_CANCEL = 0xFFFFFFFE,
            /// <summary>
            /// A serious error occured while formatting. The drive is unable to be formatted by the OS.
            /// </summary>
            SHFMT_NOFORMAT = 0xFFFFFFD
        }

        [Flags]
        private enum SHFormatOptions : uint
        { 
   
            /// <summary>
            /// Full formatting
            /// </summary>
            SHFMT_OPT_COMPLETE = 0x0,
            /// <summary>
            /// Quick Format
            /// </summary>
            SHFMT_OPT_FULL = 0x1,
            /// <summary>
            /// MS-DOS System Boot Disk
            /// </summary>
            SHFMT_OPT_SYSONLY = 0x2
        }

        #endregion

        /// <summary>
        /// Format a drive using Shell32.dll
        /// </summary>
        /// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
        /// <param name="label">label for the drive</param>
        /// <param name="quickFormat">quick formatting?</param>
        /// <returns>true if success, false if failure</returns>
        [Obsolete("Unsupported by Microsoft nowadays. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
        public static bool FormatDrive_Shell32(char driveLetter, string label = "", bool quickFormat = true)
        { 
   
            #region args check

            if (!Char.IsLetter(driveLetter))
            { 
   
                return false;
            }

            #endregion
            bool success = false;
            string drive = driveLetter + ":";
            try
            { 
   
                var di = new DriveInfo(drive);
                var bytes = Encoding.ASCII.GetBytes(di.Name.ToCharArray());
                uint driveNumber = Convert.ToUInt32(bytes[0] - Encoding.ASCII.GetBytes(new[] { 
    'A' })[0]);
                var options = SHFormatOptions.SHFMT_OPT_COMPLETE;
                if (quickFormat)
                    options = SHFormatOptions.SHFMT_OPT_FULL;

                uint returnCode = SHFormatDrive(IntPtr.Zero, driveNumber, SHFormatFlags.SHFMT_ID_DEFAULT, options);
                if (returnCode == (uint)SHFormatFlags.SHFMT_ERROR)
                    throw new Exception("An error occurred during the format. This does not indicate that the drive is unformattable.");
                else if (returnCode == (uint)SHFormatFlags.SHFMT_CANCEL)
                    throw new OperationCanceledException("The format was canceled.");
                else if (returnCode == (uint)SHFormatFlags.SHFMT_NOFORMAT)
                    throw new IOException("The drive cannot be formatted.");

                SetLabel(driveLetter, label);
                success = true;
            }
            catch (Exception) { 
    }
            return success;
        }

        #endregion

        #region FormatDrive_Win32Api

        // http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx

        /// <summary>
        /// Format a drive using Win32 API
        /// </summary>
        /// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
        /// <param name="label">label for the drive</param>
        /// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
        /// <param name="quickFormat">quick formatting?</param>
        /// <param name="enableCompression">enable drive compression?</param>
        /// <param name="clusterSize">cluster size. Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
        /// <returns>true if success, false if failure</returns>
        [Obsolete("Might have troubles formatting ram drives. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
        public static bool FormatDrive_Win32Api(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int clusterSize = 8192)
        { 
   
            #region args check

            if (!Char.IsLetter(driveLetter) ||
                !IsFileSystemValid(fileSystem))
            { 
   
                return false;
            }

            #endregion
            bool success = false;
            try
            { 
   
                var moSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_Volume WHERE DriveLetter='" + driveLetter + ":'");
                foreach (ManagementObject mo in moSearcher.Get())
                { 
   
                    mo.InvokeMethod("Format", new object[] { 
    fileSystem, quickFormat, clusterSize, label, enableCompression });
                    success = true;
                }
            }
            catch (Exception)
            { 
   
                success = false;
            }
            return success;
        }

        #endregion

        #region IsFileSystemValid

        /// <summary>
        /// test if the provided filesystem value is valid
        /// </summary>
        /// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
        /// <returns>true if valid, false if invalid</returns>
        public static bool IsFileSystemValid(string fileSystem)
        { 
   
            #region args check

            if (fileSystem == null)
            { 
   
                return false;
            }

            #endregion
            switch (fileSystem)
            { 
   
                case "FAT":
                case "FAT32":
                case "EXFAT":
                case "NTFS":
                case "UDF":
                    return true;
                default:
                    return false;
            }
        }

        #endregion
    }    
5.USB设备插拔监控
  • WMI查询语句(WQL)
private const string RemovableDiskRemovedWatcherQuery = "SELECT * FROM __InstanceDeletionEvent WITHIN 5 WHERE TargetInstance ISA \"Win32_DiskDrive\"";//设备移除监控语句
private const string RemovableDiskAddedWatcherQuery = "SELECT * FROM __InstanceCreationEvent WITHIN 15 WHERE TargetInstance ISA \"Win32_DiskDrive\"";//设备插入监控语句
private const string RemovableDiskQuery = "SELECT SerialNumber, DeviceID, Model, Description, Name, Caption, PNPDeviceID, InterfaceType, MediaType, Index, Size, Partitions, FirmwareRevision from Win32_DiskDrive Where InterfaceType = 'USB'";//当前已插入的USB设备
  • 监控设备的移除和添加

设置操作范围

ManagementScope scope = new ManagementScope("root\\CIMV2"); 

移除监控

void DiskRemovedWatcher()
{
            WqlEventQuery query = new WqlEventQuery(RemovableDiskRemovedWatcherQuery);
            _diskRemovedWatcher = new ManagementEventWatcher(scope, query);
            _diskRemovedWatcher.EventArrived += (sender, e) =>
            {
                    // occurs when a disk drive is removed
                    using (
                        ManagementBaseObject mbo =
                            e.NewEvent[WMIConstants.TargetInstanceProperty] as ManagementBaseObject)
                    {
                        if (mbo != null)
                        {
                           //todo
                        }
                    }
            };
}

添加监控

private void DiskAddedWatcher()
{
            WqlEventQuery query = new WqlEventQuery(RemovableDiskAddedWatcherQuery);
            _diskAddedWatcher = new ManagementEventWatcher(scope, query);
            _diskAddedWatcher.EventArrived += (sender, e) =>
            {
                    // occurs when a disk drive is added
                    using (
                        ManagementBaseObject mbo = e.NewEvent[WMIConstants.TargetInstanceProperty] as ManagementBaseObject)
                    {
                        if (mbo != null)
                        {
                            //todo
                        }
                    }
            };
}

当前已插入的USB设备

using (ManagementObjectSearcher mosDisks = new ManagementObjectSearcher(RemovableDiskQuery))
{       
    // Loop through each object (disk) retrieved by WMI
    foreach (ManagementObject moDisk in mosDisks.Get())
    {
       DasBasicPart disk = GetDisk(moDisk);
       disks.Add(disk);
    }
}

一个方便的WMI测试工具:
Win+R,然后输入:wbemtest.exe,回车即可。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186849.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 做测试的一定要知道EFI Shell 命令参考

    做测试的一定要知道EFI Shell 命令参考EFIShell命令参考命令说明引导命令—EFIShell 与nPartition引导有关的命令。

    2022年7月19日
    22
  • HTTP和HTTPS有什么区别? 什么是SSL证书?使用ssl证书优势?

    HTTP和HTTPS有什么区别? 什么是SSL证书?使用ssl证书优势?

    2021年10月14日
    53
  • 2268大约是多少_情态动词过去式

    2268大约是多少_情态动词过去式小 Q 在电子工艺实习课上学习焊接电路板。一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字 1,2,3… 进行标号。电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅存在一条通路(通路指连接两个元件的导线序列)。在电路板上存在一个特殊的元件称为“激发器”。当激发器工作后,产生一个激励电流,通过导线传向每一个它所连接的节点。而中间节点接收到激励电流后,得到信息,并将该激励电流传向与它连接并且尚未接收到激励电流的节点。最终,激励电流将到达一些“终止节点”——

    2022年8月9日
    3
  • 关于浏览器ip代理导致定位错乱问题的坑

    关于浏览器ip代理导致定位错乱问题的坑

    2021年10月23日
    70
  • pythonidle安装教程(python命令行安装库)

    首先,IDLE是一款免费的软件,可以直接去python的官网下载,在官网找到相应自己电脑的配置的一类之后选择“DownloadWindowsx86-64executableinstaller”这个下载就可以了。下载完成后打开安装包,1.记得勾选上“apppython3.7toPATH”这一项。然后点击next2.这一步没啥选的直接点击next3.在这一步…

    2022年4月15日
    248
  • 在flask中使用jsonify和json.dumps的区别

    在flask中使用jsonify和json.dumps的区别flask提供了jsonify函数供用户处理返回的序列化json数据,而python自带的json库中也有dumps方法可以序列化json对象,那么在flask的视图函数中return它们会有什么不同之处呢?想必开始很多人和我一样搞不清楚,只知道既然框架提供了方法就用,肯定不会错。但作为开发人员,我们需要弄清楚开发过程中各种实现方式的特点和区别,这样在我们面对不同的需求时才能做出相对合理的选择,而

    2022年5月24日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号