Xamarin.AndroidでUUIDを生成取得する

提供: MonoBook
2019年3月22日 (金) 07:49時点における122.135.115.231 (トーク)による版
ナビゲーションに移動 検索に移動

アプリ固有のIDを生成して保存しておく。 この方法だと再インストールするとIDは変わる。 リセマラ対応ゲームなどを作るときはこれ。

    public class Installation
    {
        public static string _uuid;
        public const string INSTALLATION = "INSTALLATION";

        public static string GetUUID(Context context)
        {
            if (_uuid == null) 
            {
                var installationFilePath = Path.Combine(context.FilesDir.AbsolutePath, INSTALLATION);
                if (!File.Exists(installationFilePath))
                {
                    _uuid = System.Guid.NewGuid().ToString();
                    File.WriteAllText(installationFilePath, _uuid);
                }
                _uuid = File.ReadAllText(installationFilePath);
            }
            return _uuid;
        }
    }

関連項目