{"id":19685233,"url":"https://github.com/ckjbug/gist","last_synced_at":"2026-03-03T17:34:20.857Z","repository":{"id":130416818,"uuid":"78209582","full_name":"ckjbug/Gist","owner":"ckjbug","description":"功不唐娟(5W2H)","archived":false,"fork":false,"pushed_at":"2019-09-23T08:36:57.000Z","size":3601,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-10T07:01:33.187Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ckjbug.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-06T13:50:47.000Z","updated_at":"2019-09-23T08:36:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"c1534d2e-294a-4809-b3fa-795bf9e51efc","html_url":"https://github.com/ckjbug/Gist","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckjbug%2FGist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckjbug%2FGist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckjbug%2FGist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckjbug%2FGist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ckjbug","download_url":"https://codeload.github.com/ckjbug/Gist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240993451,"owners_count":19890418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T18:21:04.224Z","updated_at":"2026-03-03T17:34:15.823Z","avatar_url":"https://github.com/ckjbug.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Csharp Gist\n\n\u003e Csharp Gist代码片段\n\n\u003e CSharp_Util_Library类库\n\n\u003e DotNet.Utilities（整理）\n\n---------\n\nC#调用CMD来执行命令\n\n```cs\n//要执行的指令，如关机：shutdown -s -t 00\nstring str = \"xxx\"；\n//开启一个新的进程用来跑CMD\nProcess p = new Process();\n//设置要启动的应用程序\np.StartInfo.FileName = \"cmd.exe\";\n//是否使用操作系统shell启动\np.StartInfo.UseShellExecute = false;\n// 接受来自调用程序的输入信息\np.StartInfo.RedirectStandardInput = true;\n//输出信息\np.StartInfo.RedirectStandardOutput = true;\n// 输出错误\np.StartInfo.RedirectStandardError = true;\n//不显示程序窗口\np.StartInfo.CreateNoWindow = true;\n//启动程序\np.Start();\n//向cmd窗口发送输入信息\np.StandardInput.WriteLine(str + \"\u0026exit\");\np.StandardInput.AutoFlush = true;\n//获取输出信息\nstring strOuput = p.StandardOutput.ReadToEnd();\n//等待程序执行完退出进程\np.WaitForExit();\np.Close();\t\t\n```                  \n\n-----------------------\n\nC#实现程序(x.exe)的重启\n\n```cs\nApplication.Exit();\nSystem.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);\t\n//或者使用Application.Restart();\n```\n\n-----------------------\n\nC#实现程序开机自动启动（通过注册表设置启动项）\n\n```cs\nusing system .microsoft.win32;//一定要引用\nRegistryKey rk = Registry.LocalMachine;\nRegistryKey rk2 = rk.CreateSubKey(@\"Software\\Microsoft\\Windows\\CurrentVersion\\Run\");\nrk2.SetValue( \"autorun\",程序路径);\nrk2.Close();\nrk.Close();\n```\n\n-----------------------\n\nC#通过调用窗口句柄进行截屏（功能更强大）\n\n```cs\n\n```\n\nC#通过Winform程序获取Windows系统桌面截屏\n\n\n```cs\n//获取主屏          \nScreen s = Screen.PrimaryScreen;\n  \n//获取系统上所有显示器的截屏          \n//Screen[] screens = Screen.AllScreens;          \n//创建一个位图,将其大小设置为何屏幕大小一致,为了获取屏幕的图片\n            \nBitmap bit = new Bitmap(s.Bounds.Width, s.Bounds.Height);\n            \n//利用当前bit获取一个画布,画布已经于Graphics对象关联\n            \nusing (Graphics g = Graphics.FromImage(bit))\n      {\n          //将屏幕的(0,0)坐标截图内容copy到画布的(0,0)位置,尺寸到校 bit.size;\n          g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bit.Size);\n          //将位图保存到D盘\n          bit.Save(@\"C:\\Users\\Enz\\Desktop\\desktop.jpg\");\n          //释放位图资源\n          bit.Dispose();\n          //释放画布\n          //g.Dispose();\n      }\n```\n\n-----------------------\n\nC#使用TcpClient/TcpListener进行异步通信操作\n\n\n```cs\npublic static class TcpHelper\n    {\n        public static async void ConnectAsTcpClient()\n        {\n\t    //声明一个通信客户单\n            using (var tcpClient = new TcpClient())\n            {\n                Console.WriteLine(\"[Client] Connecting to server\");\n                await tcpClient.ConnectAsync(\"127.0.0.1\", 1234);\n                Console.WriteLine(\"[Client] Connected to server\");\n\t\t//创建一个网络流来收发数据，也可以用内存流和文件流实现\n                using (var networkStream = tcpClient.GetStream())\n                {\n                    Console.WriteLine(\"[Client] Writing request {0}\", ClientRequestString);\n                    await networkStream.WriteAsync(ClientRequestBytes, 0, ClientRequestBytes.Length);\n\n                    var buffer = new byte[4096];\n                    var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);\n                    var response = Encoding.UTF8.GetString(buffer, 0, byteCount);\n                    Console.WriteLine(\"[Client] Server response was {0}\", response);\n                }\n            }\n        }\n\n        private static readonly string ClientRequestString = \"Some HTTP request here\";\n        private static readonly byte[] ClientRequestBytes = Encoding.UTF8.GetBytes(ClientRequestString);\n\n        private static readonly string ServerResponseString = \"\u003c?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?\u003e\u003cdocument\u003e\u003cuserkey\u003ekey\u003c/userkey\u003e \u003cmachinemode\u003e1\u003c/machinemode\u003e\u003cserial\u003e0000\u003c/serial\u003e\u003cunitname\u003eDevice\u003c/unitname\u003e\u003cversion\u003e1\u003c/version\u003e\u003c/document\u003e\\n\";\n        private static readonly byte[] ServerResponseBytes = Encoding.UTF8.GetBytes(ServerResponseString);\n\n        public static async void StartListener()\n        {\n\t    //声明一个通信服务\n            var tcpListener = TcpListener.Create(1234);\n\t    //开始监听\n            tcpListener.Start();\n\t    //等待客户端连接\n            var tcpClient = await tcpListener.AcceptTcpClientAsync();\n            Console.WriteLine(\"[Server] Client has connected\");\n\t    //创建一个网络流来收发数据\n            using (var networkStream = tcpClient.GetStream())\n            {\n                var buffer = new byte[4096];\n                Console.WriteLine(\"[Server] Reading from client\");\n                var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);\n                var request = Encoding.UTF8.GetString(buffer, 0, byteCount);\n                Console.WriteLine(\"[Server] Client wrote {0}\", request);\n                await networkStream.WriteAsync(ServerResponseBytes, 0, ServerResponseBytes.Length);\n                Console.WriteLine(\"[Server] Response has been written\");\n            }\n        }\n    }\n\nstatic void Main(string[] args)\n        {\n            TcpHelper.StartListener();\n            TcpHelper.ConnectAsTcpClient();\n            Console.ReadLine();\n        }\n\n\n```\n\n\n\n\n\n\n\n\n\n\n![DotNet.Utilities](https://github.com/ckjbug/Programming-Road/blob/master/CSharp_Util_Library.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckjbug%2Fgist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fckjbug%2Fgist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckjbug%2Fgist/lists"}