///



/// 创建桌面图标

///

public static void CreateShortcutOnDesktop(string LnkName)

{

        String shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), LnkName + ".lnk");
if (!System.IO.File.Exists(shortcutPath))
{
string AppName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);
// 获取当前应用程序目录地址
String exePath = AppDomain.CurrentDomain.BaseDirectory + AppName + ".exe";
IW.IWshShell shell = new IW.WshShell();
// 确定是否已经创建的快捷键被改名了
foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
{
IW.WshShortcut tempShortcut = (IW.WshShortcut)shell.CreateShortcut(item);
if (tempShortcut.TargetPath == exePath)
{
return;
}
}
IW.WshShortcut shortcut = (IW.WshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = exePath;
shortcut.Arguments = "";// 参数
shortcut.Description = AppName + exePath;
shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性
shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件
//shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下
shortcut.WindowStyle = 1;
shortcut.Save();
MessageBox.Show("桌面快捷方式已创建!");
}
}

public partial class App : Application

{

public App()

{

//UI线程异常

this.DispatcherUnhandledException += App_DispatcherUnhandledException;

//非UI线程异常

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

    }
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//可以记录日志并转向错误bug窗口友好提示用户
e.Handled = true;
try
{
PLogs.Error(e.Exception.TargetSite.ToString(), e.Exception.Message); }
catch (Exception)
{ }
MessageBox.Show("抱歉给您带来不便!消息:" + e.Exception.Message, "系统错误",MessageBoxButton.OK,MessageBoxImage.Error); }
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//可以记录日志并转向错误bug窗口友好提示用户
if (e.ExceptionObject is System.Exception)
{ Exception ex = (System.Exception)e.ExceptionObject;
try
{
PLogs.Error(ex.TargetSite.ToString(), ex.Message); }
catch (Exception)
{ }
MessageBox.Show("抱歉给您带来不便!消息:" + ex.Message, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error); }
}
} class PLogs
{
/// <summary>
/// 普通日志
/// </summary>
/// <param name="className">类名</param>
/// <param name="info">日志记录</param>
public static void Info(string className, string info)
{
WriteLog("INFO", className, info);
} /// <summary>
/// 警告日志
/// </summary>
/// <param name="className">类名</param>
/// <param name="info">日志记录</param>
public static void Warn(string className, string info)
{
WriteLog("WARN", className, info);
} /// <summary>
/// 错误日志
/// </summary>
/// <param name="className">类名</param>
/// <param name="info">日志记录</param>
public static void Error(string className, string info)
{
WriteLog("ERROE", className, info);
} /// <summary>
/// 写入日志
/// </summary>
/// <param name="className">类名</param>
/// <param name="infoLevel">日志级别</param>
/// <param name="info">日志记录</param>
private static void WriteLog(string className, string infoLevel, string info)
{
string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "/logs";
if (!Directory.Exists(logFilePath))
{
Directory.CreateDirectory(logFilePath);
}
string logFileName = logFilePath + "/" + "log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
if (!File.Exists(logFileName))
{
File.Create(logFileName).Close();
}
string logFormat = string.Format("[ {0} ] {1} {2} {3}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
className, infoLevel, info);
StreamWriter sw = File.AppendText(logFileName);
sw.WriteLine(logFormat);
sw.Flush();
sw.Close();
}
}

wpf 全局异常捕捉+错误日志记录+自动创建桌面图标的更多相关文章

  1. wpf 全局异常捕捉+简单日志记录

    `namespace MyApp { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { public App() ...

  2. .NET Core通过过滤器和中间件两种方式实现全局异常捕获和日志记录

    1.一共有五类过滤器IAsyncAuthorizationFilter  IAsyncResourceFilter   IAsyncActonFilter  IAsyncExceptionFilter ...

  3. 将错误日志记录在txt文本里

    引言 对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能 很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就 ...

  4. android中全局异常捕捉

    android中全局异常捕捉 只要写代码就会有bug,但是我们要想办法收集到客户的bug.有第三方bugly或者友盟等可以收集.但是,android原生就提供了有关收集异常的api,所以我们来学习一下 ...

  5. PHP错误日志记录:display_errors与log_errors的区别

    我们所做的东西,无论在开发环境还是在生产环境都可能会出现一些问题. 开发环境下,我们会要求错误尽可能详细的呈现出来,错误提示信息越详细越好,越详细越能帮助开发人员确定问题所在并从根本上解决他们. 生产 ...

  6. Spring 全局异常捕捉

    Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...

  7. springboot(四)拦截器和全局异常捕捉

    github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 全部 ...

  8. 在Spring Boot中添加全局异常捕捉提示

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 全局异常捕捉: 新建一个类GlobalDefaultExceptionHandler, 在class注解上@Controll ...

  9. PHP 捕捉错误,记录到日志

    register_shutdown_function("shutdown"); define('ERR_LOG_FILE', '/dev/shm/php_log.txt'); if ...

随机推荐

  1. 多测师讲解pyhon__hashlib_高级讲师肖sir

    一.加密,加密成16进制的字符串 # import hashlib # 导入hashlib模块# md = hashlib.md5() # 获取一个md5加密算法对象# md.update('需要加密 ...

  2. CentOS 7操作系统目录结构介绍

    CentOS 7操作系统目录结构介绍 操作系统存在着大量的数据文件信息,相应文件信息会存在于系统相应目录中,为了更好的管理数据信息,会将系统进行一些目录规划,不同目录存放不同的资源. 根下目录结构说明 ...

  3. python 爬虫 循环分页

    import osfrom time import sleepimport fakerimport requestsfrom lxml import etreefake = faker.Faker() ...

  4. php-ffmpeg 操作视频/音频文件

    php-ffmpeg 是一个php操作视频/音频文件的类库. GitHub地址:https://github.com/PHP-FFMpeg/PHP-FFMpeg/ 使用composer快速安装:com ...

  5. STM32芯片型号的命名规则

    意法半导体已经推出STM32基本型系列.增强型系列.USB基本型系列.增强型系列:新系列产品沿用增强型系列的72MHz处理频率.内存包括64KB到256KB闪存和20KB到64KB嵌入式SRAM.新系 ...

  6. js 实现 input file 文件上传

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  7. day71:drf:API接口&Restful API规范&Django Rest Framework&drf中的序列化和反序列化功能

    目录 1.web应用模式 2.API接口 3.Restful API规范 4.序列化 5.Django Rest Framework 1.drf的简单介绍 2.drf的特点 3.如何安装drf 4.d ...

  8. Linux配置阿里epl源

    去阿里云 有源仓库 阿里云镜像官方站点 https://developer.aliyun.com/mirror/ 先备份本机上的源 mv /etc/yum.repos.d/CentOS-Base.re ...

  9. vue知识点10

    今天彻底掌握了如下: 1.解决回调地狱三种方案        callback async await Promise 2.中间件(middleware)        express.static  ...

  10. 没事学些KVM(三)虚拟机基础管理

    创建完成虚拟机后,需要对虚拟机进行基础管理学习 virsh list #查看虚拟机列表 改命令只能查看正在运行或挂起的虚拟机 如果需要查看所有的虚拟机需要添加--all 参数 virsh start ...