private void ShutdownWindows() { try { ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem"); managementClass.Get(); // 権限を有効化する managementClass.Scope.Options.EnablePrivileges = true; ManagementObjectCollection managementObjectCollection = managementClass.GetInstances(); foreach (ManagementObject managementObject in managementObjectCollection) { // InvokeMethodでWMIのメソッドを実行する managementObject.InvokeMethod( "Win32Shutdown", // 強制シャットダウン指定 new object[] { 5, 0 } ); managementObject.Dispose(); } managementObjectCollection.Dispose(); managementClass.Dispose(); } catch (Exception ex) { //なんかエラーログを出力する } }
2022年10月16日日曜日
[C#,サンプルコード]Windowsをシャットダウンするのサンプルコード
参考サイト
https://johobase.com/wmi-os-shutdown-csharp/
https://www.fenet.jp/dotnet/column/language/4629/
[C#,サンプルコード]HogeHogeをkillするのサンプルコード
private void TerminateHogeHoge() { //ローカルコンピュータ上で実行されている"HogeHoge"という名前のすべてのプロセスを取得 Process[] processes = Process.GetProcessesByName("HogeHoge"); foreach (Process process in processes) { // メイン ウィンドウにクローズ メッセージを送信する bool isCloseMainWindow = process.CloseMainWindow(); if (! isCloseMainWindow) { // 終了しなかった場合は強制終了する process.Kill(); } // プロセスが終了するまで60秒間に待機する // TODO:何秒待機するのが適正化?暫定的に60秒。 bool isExit = process.WaitForExit(60000); if (isExit) { return; } throw new HogeHogeException("一定時間内にHogeHogeが終了しませんでした。"); } }
2022年10月12日水曜日
[typescript,WebAPI,サンプルコード]DeepLのWebAPIを呼び出すTypeScriptのサンプルコード
import fetch from "node-fetch"; // DeepL WebAPI URL const URL_API = "https://api-free.deepl.com/v2/translate?auth_key="; //DeepLのAPIキーを設定 ※各自のAPIキーで書き換え const apiKey = "xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx:fx"; // 翻訳対象言語 enum TARGET_LANG { Ja = "JA", // 日本語に翻訳 En = "EN", // 英語に翻訳 } // DeepLのWepAPIにRESTで翻訳依頼する const executeTranslation = async ( targetText: string, targetLang: TARGET_LANG ) => { //APIリクエストのGET時に必要なパラメータを設定 const requestUrl = URL_API + apiKey + "&text=" + targetText + "&target_lang=" + targetLang; const response = await fetch(requestUrl); console.log("response", response); const json = await response.json(); console.log("json", json); return json; }; // WebAPIの返り値(JSON)をこっち側のクラスに変換する変換するためのType type Translation = { detected_source_language: string; text: string; }; type TranslationResult = { translations: Translation[]; }; const main = async () => { const result: TranslationResult = await executeTranslation( "This is translation test.", TARGET_LANG.Ja ); console.log("英語から日本語への翻訳:result", result); console.log( "英語から日本語への翻訳:detected_source_language", result.translations[0].detected_source_language ); console.log("英語から日本語への翻訳:text", result.translations[0].text); const result2: TranslationResult = await executeTranslation( "これは翻訳テストです。", TARGET_LANG.En ); console.log("日本語から英語への翻訳:result", result2); console.log( "日本語から英語への翻訳:detected_source_language", result2.translations[0].detected_source_language ); console.log("日本語から英語への翻訳:text", result2.translations[0].text); }; main();
2022年10月11日火曜日
[npm, typescript]TypeScriptのインストールから実行まで
■前提条件
VSCodeをインストールする
■手順
1.npm init
2.npm install typescript
3.npx tsc --init
4.npx tsc
2022年10月7日金曜日
Windowsでプロセスのメモリ使用量を記録する方法
参考サイト
https://www.clear-code.com/blog/2022/3/1/windows-performance-log.html
■PDF化
https://drive.google.com/file/d/1XT0BuMsxcEBuJL2WWnWcB4MByPblSXU-/view?usp=sharing
登録:
投稿 (Atom)