2022年10月16日日曜日

[C#,サンプルコード]Windowsをシャットダウンするのサンプルコード

参考サイト https://johobase.com/wmi-os-shutdown-csharp/ https://www.fenet.jp/dotnet/column/language/4629/
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)
    {
        //なんかエラーログを出力する
    }
}

[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

2022年8月1日月曜日

[react-native,react-navigation/drawer]createDrawerNavigator()を使用した時のエラー:Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin"

 react-navigation/drawerのcreateDrawerNavigator()を使用した時のエラー対応を備忘する

■エラーメッセージ

"Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin"


■対応手順

1.babel.config.jsに【plugins: ["react-native-reanimated/plugin"]】を追記する。

[babel.config.js]

-----

module.exports = function (api) {

  api.cache(true);

  return {

    presets: ["babel-preset-expo"],

    plugins: ["react-native-reanimated/plugin"],

  };

};

-----

2.[expo start -c]コマンドを実行する


■参考サイト

https://www.youtube.com/watch?v=Ad0LUwCytu4&ab_channel=LahoreGraphicsAcademy

2022年7月24日日曜日

[typescript, react native]React Native Expo でapkファイルを作成する

 備忘録になる


■前提条件

React Native Expoアカウントを作成する

https://expo.dev/


■手順

1.Expo プロジェクトを作成する

>expo init build-sample


2. プログラミング言語を選択する

 .blank (TypeScript)


3.「expo build:android」を実行する

> npx expo build:android


4.Androidパッケージ名を決める

> What would you like your Android package name to be?

> com.example.myapp


5.ビルドタイプを決める

> Choose the build type you would like: 

> apk


6.KeyStoreを指定する

>? Would you like to upload a Keystore or have us generate one for you?

>If you don't know what this means, let us generate it! :) »

>Generate new keystore


7.expo-updatesのインストール

>In order to publish an update, expo-updates needs to be installed. Do you want to install it now? 

>Y


8.ビルドが成功したことを確認する

>Successfully built standalone app


9.apkダウンロードのURLが通知される。


2022年6月23日木曜日

オブジェクト指向エクササイズ

 オブジェクト指向エクササイズは、書籍「ThoughtWorksアンソロジー」に記載されている。

ソースコードを書くときに以下のルールを適用する。


  1. 1つのメソッドにつきインデントは1段階までにすること
  2. else句を使用しないこと
  3. すべてのプリミティブ型と文字列型をラップすること
  4. 1行につきドットは1つまでにすること
  5. 名前を省略しないこと
  6. すべてのエンティティを小さくすること
  7. 1つのクラスにつきインスタンス変数は2つまでにすること
  8. ファーストクラスコレクションを使用すること
  9. Getter、Setter、プロパティを使用しないこと

参考
https://youtu.be/Ucd-lMmDuFQ

2022年2月17日木曜日

[TypeScript,JavaScript]Study項目

以下のTypeScript/JavaScriptについて、空き時間に学習する。


・ Symbol.for()

https://nextribe.co.jp/katayan/basic-of-symbol-js


・typeof X[keyof typeof X]

https://nishinatoshiharu.com/keyof-typeof/


2022年1月4日火曜日

[Node.js]Visual Studio CodeにNode.jsのテスト環境を構築

 以下の拡張機能プラグインをインストールする



■拡張機能プラグイン

・Jest Runner

・Jest Snippets

・Jest Test Explorer

・Test Adapter Converter

・Test Explorer UI


2022年1月3日月曜日

[npm]プロジェクトの管理

 npmのプロジェクト管理の備忘を記載する。

以下の3つだけ覚えておく。


1.npm init

2.npm install

3.npm run


■参考

https://www.webdesignleaves.com/pr/jquery/npm_basic.html

[npm] コマンド

npmコマンドを備忘する

■ローカルインストール(プロジェクトのディレクトリで実行)

npm install パッケージ名


■グローバルインストール

npm install -g パッケージ名


■参考

https://www.webdesignleaves.com/pr/jquery/npm_basic.html