static void initMyTempDir() { try { deleteUnusedTempDir(); } catch { } int num = 0; while (true) { byte[] rand = Secure.Rand(2); string tmp2 = Str.ByteToStr(rand); string tmp = Path.Combine(Env.tempDir, "NET_" + tmp2); if (IO.IsDirExists(tmp) == false && IO.MakeDir(tmp)) { Env.myTempDir = tmp; break; } if ((num++) >= 100) { throw new SystemException(); } } string lockFileName = Path.Combine(Env.myTempDir, "LockFile.dat"); lockFile = IO.FileCreate(lockFileName); }
public static string GenerateInternalName(string name) { name = name.Trim().ToUpperInvariant(); return("dnmutant_" + Str.ByteToStr(Str.HashStr(name)).ToLowerInvariant()); }
// 初期化 static Env() { FrameworkVersion = Environment.Version; if (FrameworkInfoString.StartsWith(".NET Core", StringComparison.InvariantCultureIgnoreCase)) { IsDotNetCore = true; } OsInfo = Environment.OSVersion; IsWindows = (OsInfo.Platform == PlatformID.Win32NT); if (IsUnix) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { IsLinux = true; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { IsMac = true; } } PathSeparator = "" + Path.DirectorySeparatorChar; if (Str.IsEmptyStr(PathSeparator)) { PathSeparator = "/"; if (Environment.OSVersion.Platform == PlatformID.Win32NT) { PathSeparator = "\\"; } } ExeFileName = IO.RemoveLastEnMark(getMyExeFileName()); if (Str.IsEmptyStr(ExeFileName) == false) { AppRootDir = ExeFileDir = IO.RemoveLastEnMark(System.AppContext.BaseDirectory); // プログラムのあるディレクトリから 1 つずつ遡ってアプリケーションの root ディレクトリを取得する string tmp = ExeFileDir; while (true) { try { tmp = Path.GetDirectoryName(tmp); if (File.Exists(Path.Combine(tmp, "approot")) || File.Exists(Path.Combine(tmp, "appsettings.json")) || File.Exists(Path.Combine(tmp, "appsettings.Development.json"))) { AppRootDir = tmp; break; } } catch { break; } } } else { ExeFileName = "/tmp/dummyexe"; ExeFileDir = "/tmp"; AppRootDir = IO.RemoveLastEnMark(Environment.CurrentDirectory); } HomeDir = IO.RemoveLastEnMark(Kernel.GetEnvStr("HOME")); if (Str.IsEmptyStr(HomeDir)) { HomeDir = IO.RemoveLastEnMark(Kernel.GetEnvStr("HOMEDRIVE") + Kernel.GetEnvStr("HOMEPATH")); } if (Str.IsEmptyStr(HomeDir) == false) { UnixMutantDir = Path.Combine(HomeDir, ".dnmutant"); } else { HomeDir = AppRootDir; if (IsUnix) { UnixMutantDir = Path.Combine("/tmp", ".dnmutant"); } } if (IsWindows) { UnixMutantDir = ""; } if (Str.IsEmptyStr(UnixMutantDir) == false) { IO.MakeDirIfNotExists(UnixMutantDir); } if (IsWindows) { // Windows SystemDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.System)); WindowsDir = IO.RemoveLastEnMark(Path.GetDirectoryName(SystemDir)); TempDir = IO.RemoveLastEnMark(Path.GetTempPath()); WinTempDir = IO.RemoveLastEnMark(Path.Combine(WindowsDir, "Temp")); IO.MakeDir(WinTempDir); if (WindowsDir.Length >= 2 && WindowsDir[1] == ':') { WindowsDir = WindowsDir.Substring(0, 2).ToUpper(); } else { WindowsDrive = "C:"; } } else { // UNIX SystemDir = "/bin"; WindowsDir = "/bin"; WindowsDrive = "/"; if (Str.IsEmptyStr(HomeDir) == false) { TempDir = Path.Combine(HomeDir, ".dntmp"); } else { TempDir = "/tmp"; } WinTempDir = TempDir; } ProgramFilesDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)); PersonalStartMenuDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)); PersonalProgramsDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Programs)); PersonalStartupDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Startup)); PersonalAppDataDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); PersonalDesktopDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)); MyDocumentsDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); LocalAppDataDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); if (IsUnix) { // ダミーディレクトリ SystemDir = "/bin"; WindowsDir = "/bin"; WindowsDrive = "/"; ProgramFilesDir = "/bin"; PersonalStartMenuDir = Path.Combine(HomeDir, "dummy/starmenu"); PersonalProgramsDir = Path.Combine(HomeDir, "dummy/starmenu/programs"); PersonalStartupDir = Path.Combine(HomeDir, "dummy/starmenu/startup"); LocalAppDataDir = PersonalAppDataDir = Path.Combine(HomeDir, ".dnappdata"); PersonalDesktopDir = Path.Combine(HomeDir, "dummy/desktop"); MyDocumentsDir = HomeDir; } StartupCurrentDir = CurrentDir; UserName = Environment.UserName; try { UserNameEx = Environment.UserDomainName + "\\" + UserName; } catch { UserNameEx = UserName; } MachineName = Environment.MachineName; CommandLine = initCommandLine(Environment.CommandLine); IsLittleEndian = BitConverter.IsLittleEndian; ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id; IsAdmin = checkIsAdmin(); // 自分用の temp ディレクトリの初期化 try { deleteUnusedTempDir(); } catch { } int num = 0; while (true) { byte[] rand = Secure.Rand(2); string tmp2 = Str.ByteToStr(rand); string tmp = Path.Combine(Env.TempDir, "NET_" + tmp2); if (IO.IsDirExists(tmp) == false && IO.MakeDir(tmp)) { Env.MyTempDir = tmp; break; } if ((num++) >= 100) { throw new SystemException(); } } if (IsWindows) { UnixMutantDir = Env.MyTempDir; } // ロックファイルの作成 string lockFileName = Path.Combine(Env.MyTempDir, "LockFile.dat"); lockFile = IO.FileCreate(lockFileName, Env.IsUnix); }
public static string GenerateInternalName(string name) { name = name.Trim().ToLowerInvariant(); return("Cores_DotNet_Mutex_" + Str.ByteToStr(Str.HashStr(name)).ToLowerInvariant()); }