【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,回车即可。

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • rtsp视频流下载_rtmp网页视频播放器

    rtsp视频流下载_rtmp网页视频播放器在线视频流地址:rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov模拟器显示界面:学好一门语言,动手去练,半天上手,一星期炉火纯青。——专攻无人车的学长

  • Landsat 8数据介绍「建议收藏」

    Landsat 8数据介绍「建议收藏」1.简介  1.1数据简介  2013年2月11日,美国航空航天局(NASA)成功发射Landsat-8卫星。Landsat-8卫星上携带两个传感器,分别是OLI陆地成像仪(OperationalLandImager)和TIRS热红外传感器(ThermalInfraredSensor)。  Landsat-8在空间分辨率和光谱特性等方面与Landsat1-7保持了基本一致,卫星一共…

  • git commit后_git回退已经push的代码

    git commit后_git回退已经push的代码Git 返回 commit 内容

  • java h2 数据库_H2数据库介绍「建议收藏」

    java h2 数据库_H2数据库介绍「建议收藏」一、H2数据库简介1、H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时支持网络版和嵌入式版本,有比较好的兼容性,支持相当标准的sql标准,支持集群2、提供JDBC、ODBC访问接口,提供了非常友好的基于web的数据库管理界面二、在Java中操作H2数据库1、以嵌入式(本地)连接方式连接H2数据库这种连接方式默认情况下只允许有一个客户端连接到…

    2022年10月12日
  • mysql15免费注册激活码【最新永久激活】

    (mysql15免费注册激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html…

  • idea 快速搭建SpringBoot项目「建议收藏」

    idea 快速搭建SpringBoot项目「建议收藏」环境:IntelliJIDEA2021.2.1+apache-maven-3.8.4+JDK8+SpringBoot2.6.21、首先新建项目File->New->Project2、选择SpringInitializr,天蝎项目信息,选择JDK8,Next3、选择SpringBoot版本,勾选上SpringWeb,点击Finish,项目就创建好了4、可以看到这是生成SpringbootDemoApplication和application.p

发表回复

您的电子邮箱地址不会被公开。

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