FileWatcher/code/src/Utils.cs

77 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileWatcher
{
class Utils
{
public static string ProgramFilesx86()
{
if (8 == IntPtr.Size
|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
public static string ProgramFilesx64()
{
return Environment.GetEnvironmentVariable("ProgramW6432");
}
/// <summary>
/// Gets the program default, Program Fils(x86) when program is compiled
/// for 32 bit Windows, and Program Files when program is compiled for
/// 64bit Windows.
/// </summary>
/// <returns></returns>
public static string ProgramFiles()
{
return Environment.GetEnvironmentVariable("ProgramFiles");
}
/// <summary>
/// Returns Windows/System32 or Windows/SysWOW64 depending on your program
/// </summary>
/// <returns></returns>
public static string SystemDirectory()
{
return Environment.SystemDirectory;
}
/// <summary>
/// Creates a relative path from one file or folder to another.
/// </summary>
/// <param name="fromPath">Contains the directory that defines the start of the relative path.</param>
/// <param name="toPath">Contains the path that defines the endpoint of the relative path.</param>
/// <param name="dontEscape">Boolean indicating whether to add uri safe escapes to the relative path</param>
/// <returns>The relative path from the start directory to the end path.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static String MakeRelativePath(String fromPath, String toPath)
{
if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
if (String.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");
// If path doesn't end with '\\' it's assumed to be a file.
if (fromPath.EndsWith("\\") == false)
{
fromPath += "\\";
}
Uri fromUri = new Uri(fromPath);
Uri toUri = new Uri(toPath);
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
return relativePath.Replace('/', Path.DirectorySeparatorChar);
}
}
}