2019-12-04 15:42:30 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace RVIO
|
|
|
|
|
|
{
|
|
|
|
|
|
[Flags]
|
|
|
|
|
|
[ComVisible(true)]
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
|
public enum FileAttributes
|
|
|
|
|
|
{
|
|
|
|
|
|
ReadOnly = 1,
|
|
|
|
|
|
Hidden = 2,
|
|
|
|
|
|
System = 4,
|
|
|
|
|
|
Directory = 16,
|
|
|
|
|
|
Archive = 32,
|
|
|
|
|
|
Device = 64,
|
|
|
|
|
|
Normal = 128,
|
|
|
|
|
|
Temporary = 256,
|
|
|
|
|
|
SparseFile = 512,
|
|
|
|
|
|
ReparsePoint = 1024,
|
|
|
|
|
|
Compressed = 2048,
|
|
|
|
|
|
Offline = 4096,
|
|
|
|
|
|
NotContentIndexed = 8192,
|
|
|
|
|
|
Encrypted = 16384,
|
|
|
|
|
|
}
|
|
|
|
|
|
public static class Error
|
|
|
|
|
|
{
|
|
|
|
|
|
public static int GetLastError()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Marshal.GetLastWin32Error();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public static class Unix
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
public static bool IsUnix
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
int p = (int)Environment.OSVersion.Platform;
|
|
|
|
|
|
return ((p == 4) || (p == 6) || (p == 128));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class FileInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Name;
|
|
|
|
|
|
public string FullName;
|
|
|
|
|
|
public long LastWriteTime;
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public long LastAccessTime;
|
|
|
|
|
|
public long CreationTime;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public long Length;
|
|
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public FileInfo(string name, string fullName)
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = name;
|
|
|
|
|
|
FullName = fullName;
|
|
|
|
|
|
}
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
public FileInfo(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
FullName = path;
|
|
|
|
|
|
Name = Path.GetFileName(path);
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.FileInfo fi = new System.IO.FileInfo(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
if (!fi.Exists) return;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
Length = fi.Length;
|
2023-04-21 15:04:31 -04:00
|
|
|
|
try { LastWriteTime = fi.LastWriteTimeUtc.Ticks; } catch { LastWriteTime = 0; }
|
|
|
|
|
|
try { LastAccessTime = fi.LastAccessTimeUtc.Ticks; } catch { LastAccessTime = 0; }
|
|
|
|
|
|
try { CreationTime = fi.CreationTimeUtc.Ticks; } catch { CreationTime = 0; }
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class DirectoryInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Name;
|
|
|
|
|
|
public string FullName;
|
|
|
|
|
|
public long LastWriteTime;
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public long LastAccessTime;
|
|
|
|
|
|
public long CreationTime;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public DirectoryInfo(string name, string fullName)
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = name;
|
|
|
|
|
|
FullName = fullName;
|
|
|
|
|
|
}
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public DirectoryInfo(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
FullName = path;
|
|
|
|
|
|
Name = Path.GetFileName(path);
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.DirectoryInfo fi = new System.IO.DirectoryInfo(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
if (!fi.Exists) return;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
try { LastWriteTime = fi.LastWriteTimeUtc.Ticks; } catch { LastWriteTime = 0; }
|
|
|
|
|
|
try { LastAccessTime = fi.LastAccessTimeUtc.Ticks; } catch { LastAccessTime = 0; }
|
|
|
|
|
|
try { CreationTime = fi.CreationTimeUtc.Ticks; } catch { CreationTime = 0; }
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
public DirectoryInfo[] GetDirectories()
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
List<DirectoryInfo> dirs = new List<DirectoryInfo>();
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(NameFix.AddLongPathPrefix(FullName));
|
|
|
|
|
|
if (!di.Exists)
|
|
|
|
|
|
return dirs.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
System.IO.DirectoryInfo[] arrDi = di.GetDirectories();
|
2019-12-04 15:42:30 -08:00
|
|
|
|
foreach (System.IO.DirectoryInfo tDi in arrDi)
|
|
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
DirectoryInfo lDi = new DirectoryInfo(tDi.Name, Path.Combine(FullName, tDi.Name));
|
|
|
|
|
|
try { lDi.LastWriteTime = tDi.LastWriteTimeUtc.Ticks; } catch { lDi.LastWriteTime = 0; }
|
|
|
|
|
|
try { lDi.LastAccessTime = tDi.LastAccessTimeUtc.Ticks; } catch { lDi.LastAccessTime = 0; }
|
|
|
|
|
|
try { lDi.CreationTime = tDi.CreationTimeUtc.Ticks; } catch { lDi.CreationTime = 0; }
|
|
|
|
|
|
|
|
|
|
|
|
dirs.Add(lDi);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
catch
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
return dirs.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
public FileInfo[] GetFiles(string SearchPattern = "*")
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
List<FileInfo> files = new List<FileInfo>();
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(NameFix.AddLongPathPrefix(FullName));
|
|
|
|
|
|
if (!di.Exists)
|
|
|
|
|
|
return files.ToArray();
|
|
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
System.IO.FileInfo[] arrDi = di.GetFiles(SearchPattern);
|
|
|
|
|
|
foreach (System.IO.FileInfo tDi in arrDi)
|
|
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
FileInfo lFi = new FileInfo(tDi.Name, Path.Combine(FullName, tDi.Name)) { Length = tDi.Length };
|
|
|
|
|
|
try { lFi.LastWriteTime = tDi.LastWriteTimeUtc.Ticks; } catch { lFi.LastWriteTime = 0; }
|
|
|
|
|
|
try { lFi.LastAccessTime = tDi.LastAccessTimeUtc.Ticks; } catch { lFi.LastAccessTime = 0; }
|
|
|
|
|
|
try { lFi.CreationTime = tDi.CreationTimeUtc.Ticks; } catch { lFi.CreationTime = 0; }
|
|
|
|
|
|
|
|
|
|
|
|
files.Add(lFi);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
catch
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return files.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public static class Directory
|
|
|
|
|
|
{
|
|
|
|
|
|
public static bool Exists(string path)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
return System.IO.Directory.Exists(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2021-01-29 17:18:28 -08:00
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public static void Move(string sourceDirName, string destDirName)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.Directory.Move(NameFix.AddLongPathPrefix(sourceDirName), NameFix.AddLongPathPrefix(destDirName));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2021-01-29 17:18:28 -08:00
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public static void Delete(string path)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.Directory.Delete(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void CreateDirectory(string path)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.Directory.CreateDirectory(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class File
|
|
|
|
|
|
{
|
|
|
|
|
|
public static bool Exists(string path)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
return System.IO.File.Exists(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
public static void Copy(string sourceFileName, string destfileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
Copy(sourceFileName, destfileName, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.File.Copy(NameFix.AddLongPathPrefix(sourceFileName), NameFix.AddLongPathPrefix(destFileName), overwrite);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2021-01-29 17:18:28 -08:00
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public static void Move(string sourceFileName, string destFileName)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.File.Move(NameFix.AddLongPathPrefix(sourceFileName), NameFix.AddLongPathPrefix(destFileName));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
}
|
2020-04-03 13:19:21 -07:00
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
public static void Delete(string path)
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.File.Delete(NameFix.AddLongPathPrefix(path));
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public static void WriteAllBytes(string path, byte[] data)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.File.WriteAllBytes(NameFix.AddLongPathPrefix(path), data);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void WriteAllText(string path, string contents)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.IO.File.WriteAllText(NameFix.AddLongPathPrefix(path), contents);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 17:18:28 -08:00
|
|
|
|
public static bool SetAttributes(string path, FileAttributes fileAttributes)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
System.IO.File.SetAttributes(NameFix.AddLongPathPrefix(path), (System.IO.FileAttributes)fileAttributes);
|
|
|
|
|
|
return true;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2021-01-29 17:18:28 -08:00
|
|
|
|
catch (Exception)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
return false;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-29 17:18:28 -08:00
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static StreamWriter? CreateText(string filename)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
int errorCode = FileStream.OpenFileWrite(filename, out Stream? fStream);
|
|
|
|
|
|
return errorCode != 0 ? null : new StreamWriter(fStream!);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static StreamReader? OpenText(string filename, Encoding Enc)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
int errorCode = FileStream.OpenFileRead(filename, out Stream? fStream);
|
|
|
|
|
|
return errorCode != 0 ? null : new StreamReader(fStream!, Enc);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
|
|
|
|
|
|
public static string ReadAllText(string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
return System.IO.File.ReadAllText(NameFix.AddLongPathPrefix(filename));
|
|
|
|
|
|
}
|
|
|
|
|
|
public static byte[] ReadAllBytes(string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
return System.IO.File.ReadAllBytes(NameFix.AddLongPathPrefix(filename));
|
|
|
|
|
|
}
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class Path
|
|
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
private const char DirectorySeparatorChar = '\\';
|
|
|
|
|
|
private const char AltDirectorySeparatorChar = '/';
|
|
|
|
|
|
private const char VolumeSeparatorChar = ':';
|
|
|
|
|
|
|
|
|
|
|
|
public static char DirSeparatorChar
|
|
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
get { return Unix.IsUnix ? AltDirectorySeparatorChar : DirectorySeparatorChar; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string FixSlash(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !Unix.IsUnix ? path : path.Replace(DirectorySeparatorChar, AltDirectorySeparatorChar);
|
2021-01-29 17:18:28 -08:00
|
|
|
|
}
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
public static string GetExtension(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return System.IO.Path.GetExtension(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static string Combine(string path1, string path2)
|
|
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
if (Unix.IsUnix)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
return System.IO.Path.Combine(path1, path2);
|
|
|
|
|
|
|
|
|
|
|
|
if (path1 == null || path2 == null)
|
|
|
|
|
|
throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
|
|
|
|
|
|
//CheckInvalidPathChars(path1);
|
|
|
|
|
|
//CheckInvalidPathChars(path2);
|
|
|
|
|
|
|
|
|
|
|
|
if (path2.Length == 0)
|
|
|
|
|
|
return path1;
|
|
|
|
|
|
|
|
|
|
|
|
if (path1.Length == 0)
|
|
|
|
|
|
return path2;
|
|
|
|
|
|
|
|
|
|
|
|
if (IsPathRooted(path2))
|
|
|
|
|
|
return path2;
|
|
|
|
|
|
|
|
|
|
|
|
char ch = path1[path1.Length - 1];
|
|
|
|
|
|
if (ch != DirectorySeparatorChar && ch != AltDirectorySeparatorChar && ch != VolumeSeparatorChar)
|
|
|
|
|
|
return path1 + DirectorySeparatorChar + path2;
|
|
|
|
|
|
return path1 + path2;
|
|
|
|
|
|
}
|
|
|
|
|
|
private static bool IsPathRooted(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (path != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//CheckInvalidPathChars(path);
|
|
|
|
|
|
|
|
|
|
|
|
int length = path.Length;
|
|
|
|
|
|
if (
|
2021-01-29 17:18:28 -08:00
|
|
|
|
(length >= 1 && (path[0] == DirectorySeparatorChar || path[0] == AltDirectorySeparatorChar)) ||
|
2019-12-04 15:42:30 -08:00
|
|
|
|
(length >= 2 && path[1] == VolumeSeparatorChar)
|
2021-01-29 17:18:28 -08:00
|
|
|
|
) return true;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetFileNameWithoutExtension(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return System.IO.Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetFileName(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return System.IO.Path.GetFileName(path);
|
|
|
|
|
|
}
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static string? GetDirectoryName(string path)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
return System.IO.Path.GetDirectoryName(path);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class FileStream
|
|
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static Stream? OpenFileRead(string path, out int result)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
result = OpenFileRead(path, out Stream? stream);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
return stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static int OpenFileRead(string path, out Stream? stream)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
stream = new System.IO.FileStream(NameFix.AddLongPathPrefix(path), FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
return 0;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
catch (Exception e)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
stream = null;
|
2024-02-28 21:59:13 -05:00
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
2023-04-21 15:04:31 -04:00
|
|
|
|
return e.HResult;
|
2024-02-28 21:59:13 -05:00
|
|
|
|
#else
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
#endif
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static int OpenFileWrite(string path, out Stream? stream)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
try
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2021-01-29 17:18:28 -08:00
|
|
|
|
stream = new System.IO.FileStream(NameFix.AddLongPathPrefix(path), FileMode.Create, FileAccess.ReadWrite);
|
|
|
|
|
|
return 0;
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
catch (Exception e)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
stream = null;
|
2024-02-28 21:59:13 -05:00
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
2023-04-21 15:04:31 -04:00
|
|
|
|
return e.HResult;
|
2024-02-28 21:59:13 -05:00
|
|
|
|
#else
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
#endif
|
2019-12-04 15:42:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class NameFix
|
|
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
public static string AddLongPathPrefix(string path)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
2023-04-21 15:04:31 -04:00
|
|
|
|
if (Unix.IsUnix)
|
2021-01-29 17:18:28 -08:00
|
|
|
|
return path;
|
|
|
|
|
|
|
2019-12-04 15:42:30 -08:00
|
|
|
|
if (string.IsNullOrEmpty(path) || path.StartsWith(@"\\?\"))
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
|
|
|
|
if (path.StartsWith(@"\\"))
|
|
|
|
|
|
return @"\\?\UNC\" + path.Substring(2);
|
|
|
|
|
|
|
|
|
|
|
|
string retPath = path;
|
|
|
|
|
|
if (path.Substring(1, 1) != ":")
|
|
|
|
|
|
retPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), retPath);
|
|
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
retPath = CleanDots(retPath);
|
2019-12-04 15:42:30 -08:00
|
|
|
|
|
|
|
|
|
|
return @"\\?\" + retPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-21 15:04:31 -04:00
|
|
|
|
private static string CleanDots(string path)
|
2019-12-04 15:42:30 -08:00
|
|
|
|
{
|
|
|
|
|
|
string retPath = path;
|
|
|
|
|
|
while (retPath.Contains(@"\..\"))
|
|
|
|
|
|
{
|
|
|
|
|
|
int index = retPath.IndexOf(@"\..\");
|
|
|
|
|
|
string path1 = retPath.Substring(0, index);
|
|
|
|
|
|
string path2 = retPath.Substring(index + 4);
|
|
|
|
|
|
|
|
|
|
|
|
int path1Back = path1.LastIndexOf(@"\");
|
|
|
|
|
|
|
|
|
|
|
|
retPath = path1.Substring(0, path1Back + 1) + path2;
|
|
|
|
|
|
}
|
|
|
|
|
|
return retPath;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-21 15:04:31 -04:00
|
|
|
|
}
|