///



/// 创建桌面图标

///

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. Vue结合Django-Rest-Frameword结合实现登录认证(二)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/2436173500265335 微信公众 ...

  2. CMD/ENTROYPOINT区别

    CMD/ENTROYPOINT区别 相同点:都是指定一个容器:启动时要运行的命令 不同点(重点): CMD: dockerfile中可以有多个CMD指令,但是只有最后一个生效,CMD会被docker ...

  3. golang 进行grpc调用

    参考https://blog.csdn.net/qq_32744005/article/details/105606383 go get google.golang.org/grpc go get - ...

  4. 炉石传说酒馆战棋一键拔线(windows)

    小编的业余游戏之一<炉石传说>,这里分享的是现在很火的游戏拔线(跳过约20秒的战斗动画),用夜吹的话说,注意,不是"日你大坝",是"整活",哈哈.小 ...

  5. protoc-c 安装记录

    记录下 protobuf-c 安装过程中的问题. 1) 安装的时候没细看依赖. --  protobuf-c requires a C compiler, a C++ compiler, protob ...

  6. 安装JDK及环境变量配置

    1.下载JDK: 下载地址:https://www.oracle.com/technetwork/java/javase/overview/index.html 2.解压,运行安装包,下一步,选择安装 ...

  7. 运行shell文件时提示/bin/bash^M: bad interpreter: 没有那个文件

    查看脚本文件是dos格式还是unix格式的几种办法.(1)cat -A filename 从显示结果可以判断,dos格式的文件行尾为^M$,unix格式的文件行尾为$:(2)od -t x1 file ...

  8. IL角度理解C#中字段,属性与方法的区别

    IL角度理解C#中字段,属性与方法的区别 1.字段,属性与方法的区别 字段的本质是变量,直接在类或者结构体中声明.类或者结构体中会有实例字段,静态字段等(静态字段可实现内存共享功能,比如数学上的pi就 ...

  9. sentinel控制台的使用

    一,下载sentinel控制台:sentinel-dashboard-1.7.0.jar , 注 1.7.1版本控制台与最新的sentinel有冲突,会报invalid type错误 二,启动sent ...

  10. Vue-cli3以上安装jquery

    vue-cli3以上就没有webpack.config.js这个文件了,所以在安装jquery时 终端执行  npm install jquery --save 之后查看package.json 安装 ...