【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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 一步一步写算法(检查表)

    一步一步写算法(检查表)

    2021年12月30日
    33
  • 《提问的艺术》读后感「建议收藏」

    《提问的艺术》读后感「建议收藏」前言提问前他明明能帮到我却不帮我提问前必知必会的一些事关于搜索引擎提问时找准对象学会停顿组织你的问题清晰的发问低声下气代替不了做自己的家庭作业删除无意义的要求不要把问题标记为紧急即使对你而言的确如此礼貌总是有益的对待无礼提问禁区总结前言众所周知,你所提技术问题的解答很大程度上取决于你提问的方式与解决此问题的难度,但是怎么清楚的让有经验的人明白你表述的问题,让你获得最

    2022年6月23日
    20
  • pycharm运行没结果_pycharm安装lxml库

    pycharm运行没结果_pycharm安装lxml库原因是用程序选择了console来运行,取消console方法如下:Run-&gt;EditConfigurations取消runwithpythonconsole的勾

    2022年8月28日
    0
  • Intent常用flag之FLAG_ACTIVITY_CLEAR_TOP

    Intent常用flag之FLAG_ACTIVITY_CLEAR_TOP先说Activity的四种启动模式:1:standard:默认模式,不需要配置LaunchMode,默认在从Activity1跳转到Activity2,然后从Activity2跳转到Activity3,此时不管桟中有没有Activity2、3,程序都会新创建一个新的Activity,最后依次返回,会按照倒叙的顺序依次退出,类似退桟的过程。    2:singleTop:singl

    2022年7月17日
    9
  • 利用MDK软件生成bin文件的简单方法

    利用MDK软件生成bin文件的简单方法一、缘由:之前学习KeilMDK-ARM软件,找了好久生成bin文件的方法,这次分享最简单的,所以写了此篇博文二、操作步骤:1、打开“KeilMDK-ARM软件”-找到魔术棒“Optionsfortarget…”:2、点击“User”选择AfterBulid/Rebuild状态下的“▢Run#1”:3、点击后面的空白处,写入命令,,最后关闭窗口,重新编译软件,即可生成bin文件:4、具体命令如下:命令格式1:fromelf.exe–bin-o“%L@L.

    2022年10月20日
    0
  • 画完三角形再画谢尔宾斯基地毯

    画完三角形再画谢尔宾斯基地毯照样废话不说,看代码看注释importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Toolkit;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;import…

    2022年7月13日
    13

发表回复

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

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