77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace BITKit
|
|
{
|
|
public class PathHelper
|
|
{
|
|
private static string appDataPath = _appDataPath;
|
|
[ExcuteOnAwake]
|
|
public static void Init()
|
|
{
|
|
if (string.IsNullOrEmpty(appDataPath))
|
|
{
|
|
appDataPath = Path.Combine(Environment.CurrentDirectory, BITApp.AppName);
|
|
}
|
|
|
|
BIT4Log.Log<PathHelper>($"配置文件路径:{appDataPath}");
|
|
DirectoryInfo directoryInfo = new(appDataPath);
|
|
directoryInfo.Create();
|
|
}
|
|
|
|
public static string GetFolderPath(params string[] paths)
|
|
{
|
|
var path = GetPath(paths);
|
|
if (path is not null)
|
|
{
|
|
DirectoryInfo directoryInfo = new(path);
|
|
directoryInfo.Create();
|
|
Directory.CreateDirectory(path);
|
|
return path;
|
|
}
|
|
|
|
return GetPath(paths);
|
|
}
|
|
public static void EnsureDirectoryCreated(string path)
|
|
{
|
|
path = Path.GetDirectoryName(path);
|
|
if (Directory.Exists(path)) return;
|
|
if (path != null) Directory.CreateDirectory(path);
|
|
}
|
|
|
|
public static string GetFilePath(params string[] paths)
|
|
{
|
|
var path = GetPath(paths);
|
|
var dictionaryPath = Path.GetDirectoryName(path);
|
|
Directory.CreateDirectory(dictionaryPath);
|
|
if (File.Exists(path)) return path;
|
|
using var fs = File.Create(path);
|
|
fs.Close();
|
|
fs.Dispose();
|
|
|
|
return path;
|
|
}
|
|
|
|
public static string GetTempFilePath(string fileName = null)
|
|
{
|
|
var path = GetFilePath("Temps", fileName ?? Guid.NewGuid().ToString());
|
|
return path;
|
|
}
|
|
|
|
public static string GetPath(params string[] paths)
|
|
{
|
|
return Path.Combine(appDataPath, Path.Combine(paths));
|
|
}
|
|
|
|
public static bool TryGetText(string path, out string text)
|
|
{
|
|
path = Path.Combine(appDataPath, path);
|
|
text = string.Empty;
|
|
if (!File.Exists(path)) return false;
|
|
text = File.ReadAllText(path);
|
|
return true;
|
|
}
|
|
private static string _appDataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
BITApp.AppName);
|
|
}
|
|
} |