//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System.IO;
namespace GameFramework
{
public static partial class Utility
{
///
/// 路径相关的实用函数。
///
public static class Path
{
///
/// 获取规范的路径。
///
/// 要规范的路径。
/// 规范的路径。
public static string GetRegularPath(string path)
{
if (path == null)
{
return null;
}
return path.Replace('\\', '/');
}
///
/// 获取远程格式的路径(带有file:// 或 http:// 前缀)。
///
/// 原始路径。
/// 远程格式路径。
public static string GetRemotePath(string path)
{
string regularPath = GetRegularPath(path);
if (regularPath == null)
{
return null;
}
return regularPath.Contains("://") ? regularPath : ("file:///" + regularPath).Replace("file:////", "file:///");
}
///
/// 移除空文件夹。
///
/// 要处理的文件夹名称。
/// 是否移除空文件夹成功。
public static bool RemoveEmptyDirectory(string directoryName)
{
if (string.IsNullOrEmpty(directoryName))
{
throw new GameFrameworkException("Directory name is invalid.");
}
try
{
if (!Directory.Exists(directoryName))
{
return false;
}
// 不使用 SearchOption.AllDirectories,以便于在可能产生异常的环境下删除尽可能多的目录
string[] subDirectoryNames = Directory.GetDirectories(directoryName, "*");
int subDirectoryCount = subDirectoryNames.Length;
foreach (string subDirectoryName in subDirectoryNames)
{
if (RemoveEmptyDirectory(subDirectoryName))
{
subDirectoryCount--;
}
}
if (subDirectoryCount > 0)
{
return false;
}
if (Directory.GetFiles(directoryName, "*").Length > 0)
{
return false;
}
Directory.Delete(directoryName);
return true;
}
catch
{
return false;
}
}
}
}
}