Files
BeWoPlaner/SharedLauncher/Extensions/PathExtensions.cs
rene 421fae3332 Feature: Download Server ignoreren, wenn nicht vorhanden
Client macht vorm Update Checken eine Anfrage an den Download Server und prüft dessen Verfügbarkeit. Sollte keine vorhanden sein, so wird der Client direkt gestartet.
2023-01-09 17:28:56 +01:00

90 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Logic;
namespace BS.SharedLauncher.Extensions
{
public static class PathExtensions
{
public static string Combine(this string start, string sub)
{
return Path.Combine(start, sub);
}
public static void CreateFolder(this string path)
{
Directory.CreateDirectory(path);
}
public static void DeleteFolder(this string path)
{
Directory.Delete(path);
}
public static bool Exists(this string path)
{
try
{
FileAttributes attr = File.GetAttributes(path);
if (attr.HasFlag(FileAttributes.Directory))
{
return Directory.Exists(path);
}
return File.Exists(path);
}
catch (FileNotFoundException)
{
}
catch (DirectoryNotFoundException)
{
}
catch (Exception)
{
}
return false;
}
public static bool IsFolderEmpty(this string path)
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
public static void ClearFolder(this string path) => ClearFolder(new DirectoryInfo(path));
public static void ClearFolder(this DirectoryInfo di)
{
foreach (FileInfo file in di.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.EnumerateDirectories())
{
dir.Delete(true);
}
}
public static void Lock(this string path, bool skiplock)
{
var file = Path.Combine(path, BeWoPathFileConfig.LockFileName);
if (!skiplock && File.Exists(file))
throw new ArgumentException($"Lock ist bereits gesetzt", path);
using (File.Create(file));
}
public static void Unlock(this string path, bool skiplock)
{
var file = Path.Combine(path, BeWoPathFileConfig.LockFileName);
if (!skiplock && !File.Exists(file))
throw new ArgumentException($"Unlock nicht möglich, Lock existiert nicht", path);
File.Delete(file);
}
}
}