JAVA转C#开发笔记
版本发布
M1.使用WinRar创建安装文件;
S1.选择Debug/Release目录下所有文件,添加到压缩文件
S2.创建自解压格式压缩文件
S3.自解压选项设置
调试部分
代码调试手段
S1.控制台输出Log工具;
Console.WriteLine("Console log...");
Debug.WriteLine("Debug log...");
Trace.WriteLine("Trace log...");
S2. Assert终止运行弹窗显示错误信息方式;
Debug.Assert(a < 50, "params error", "error code XCODE0023516");
Trace.Assert(a > 50, "params error", "error code XCODE0023536");
S3. try{}catch(e){e.XXX;e.Message}精确捕获异常位置;
C#/JAVA/JS捕获异常类似;
S4.本地LOG记录日志;
备注:
using System.Diagnostics; // Debug/Trace
VS开发环境断点调试
S1.调试相关快捷键;
|
功能 |
快捷键 |
调试 |
单步 |
F11 |
运行调试 |
F5 |
|
运行 |
CTRL + F5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其他调试手段
S1.进程停止运行;
启动任务管理器 => 进程 => 相应进程 => 创建转存文件
生成.dump的转存文件,可以用VisualStudio打开分析。
C#测试代码笔记
CMD命令工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace ConsoleApp.Utils { /// <summary> /// C#命令工具类,执行CMD命令 /// /// </summary> class CmdUtils { public static void TestCmdApi() { //ExecuteCMD(@"C:\Windows\System32\cmd.exe","ipconfig &exit"); //ExecuteCMD(@"C:\Windows\System32\calc.exe", ""); //ExecuteCMD("ipconfig &exit"); // 杀死进程 // http://www.cnblogs.com/klchang/p/6257434.html //ExecuteCMD("taskkill /im calc.exe -f &exit"); // 杀死指定名称进程 //ExecuteCMD("taskkill /pid 16620 -f &exit"); // 杀死指定PID进程 // KillProcess("calc"); } // ----------------------------------CMD命令执行API // http://www.cnblogs.com/babycool/p/3570648.html // http://www.cnblogs.com/njl041x/p/3881550.html // http://www.cnblogs.com/vaevvaev/p/7115721.html /// <summary> /// 执行cmd命令 /// 多命令请使用批处理命令连接符: /// <![CDATA[ /// &:同时执行两个命令 /// |:将上一个命令的输出,作为下一个命令的输入 /// &&:当&&前的命令成功时,才执行&&后的命令 /// ||:当||前的命令失败时,才执行||后的命令]]> /// </summary> /// <param name="cmdExe">指定应用程序的完整路径</param> /// <param name="cmdStr">执行命令行参数</param> public static bool ExecuteCMD(string cmdExe, string cmdStr) { bool result = false; try { using (Process myPro = new Process()) { //指定启动进程是调用的应用程序和命令行参数 ProcessStartInfo psi = new ProcessStartInfo(cmdExe); psi.UseShellExecute = false; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.CreateNoWindow = true; // 不显示程序窗口 myPro.StartInfo = psi; myPro.Start(); myPro.StandardInput.WriteLine(cmdStr); myPro.StandardInput.AutoFlush = true; //string res = myPro.StandardOutput.ReadToEnd(); //Console.WriteLine(res); //myPro.WaitForExit(); result = true; } } catch (Exception e) { Console.WriteLine(e.Message); } return result; } /// <summary> /// 运行cmd命令 /// 不显示命令窗口 /// </summary> /// <param name="cmdStr">执行命令行参数</param> public static bool ExecuteCMD(string cmdStr) { bool result = false; try { using (Process myPro = new Process()) { myPro.StartInfo.FileName = "cmd.exe"; myPro.StartInfo.UseShellExecute = false; myPro.StartInfo.RedirectStandardInput = true; myPro.StartInfo.RedirectStandardOutput = true; myPro.StartInfo.RedirectStandardError = true; myPro.StartInfo.CreateNoWindow = true; myPro.Start(); myPro.StandardInput.WriteLine(cmdStr); myPro.StandardInput.AutoFlush = true; string res = myPro.StandardOutput.ReadToEnd(); Console.WriteLine(res); myPro.WaitForExit(); result = true; } } catch(Exception e) { Console.WriteLine(e.Message); } return result; } /// <summary> /// C#杀死指定名称进程 失败 /// </summary> /// <param name="proName"></param> public static void KillProcess1(string proName) { Process[] ps = System.Diagnostics.Process.GetProcessesByName(proName); foreach(Process p in ps) { p.Kill(); } } /// <summary> /// C#杀死指定名称进程 /// http://blog.csdn.net/daliaojie/article/details/17244283 /// </summary> /// <param name="processName"></param> public static void KillProcess(string processName) { //获得进程对象,以用来操作 System.Diagnostics.Process myproc = new System.Diagnostics.Process(); //得到所有打开的进程 try { //获得需要杀死的进程名 foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName)) { //立即杀死进程 thisproc.Kill(); } } catch (Exception Exc) { throw new Exception("", Exc); } } } }
JSON解析
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace ConsoleApp.FrameLib { /// <summary> /// JSON数据解析测试 /// Newtonsoft.Json /// https://github.com/JamesNK/Newtonsoft.Json /// http://www.newtonsoft.com/json /// http://json.codeplex.com/ /// /// http://www.cnblogs.com/ambar/archive/2010/07/13/parse-json-via-csharp.html /// </summary> class FrameJson { private static void TestJsonParseApi() { // 对象转成string测试 Product product = new Product(); product.Name = "Apple"; product.Expiry = new DateTime(2008, 12, 28); product.Sizes = new string[] { "Small" }; string resJson = JsonUtils.ObjectToJsonString(product); Console.WriteLine(resJson); // json string转对象测试 Object obj = JsonConvert.DeserializeObject<Product>(resJson); Console.WriteLine(obj); } public static void DebugJsonApi() { TestJsonParseApi(); } } class JsonUtils { // 对象转为JSON字符串 public static string ObjectToJsonString(Object obj) { string jsonStr = JsonConvert.SerializeObject(obj); return jsonStr; } // JSON字符串转对象 public static Object ObjectToJsonString(String jsonStr) { Object obj = JsonConvert.DeserializeObject<Object>(jsonStr); return obj; } } class Product { public string Name{get;set;} public DateTime Expiry; public string[] Sizes; } }
WPF基础
1.XAML命名空间
<Window x:Class="WpfApp.Library.Basic.Ba01XamlNamespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Ba01XamlNamespace" Height="300" Width="300"> <!-- WPF命名空间 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" XAML命名空间 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 系统名称空间 xmlns:sys="clr-namespace:System;assembly=mscorlib" --> <!-- 使用系统命名空间声明一个数字字符串 --> <Window.Resources> <ResourceDictionary> <x:Array Type="sys:String" x:Key="localArray"> <sys:String>"AAA"</sys:String> <sys:String>"BBB"</sys:String> <sys:String>"CCC"</sys:String> </x:Array> </ResourceDictionary> </Window.Resources> <Grid> </Grid> </Window>