Files
DP44/Common/DTS.Common.Utilities/DiskUtility.cs
2026-04-17 14:55:32 -04:00

139 lines
5.0 KiB
C#

/*
* DiskUtility.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace DTS.Common.Utilities
{
/// <summary>
/// A collection of handy disk-related methods.
/// </summary>
public class DiskUtility : Exceptional
{
/// <summary>
/// substitutes doubles from a file into the list of doubles starting at 0 and going until either the list is out of numbers
/// or the file is
/// originally created for 29654 dp 4.0 476 3D IRTRACC DATA does not match expected data when injecting dc input per test steps
/// </summary>
/// <param name="list">list to substitute values into, this as a ref to make it obvious it will be modified</param>
/// <param name="fileName">file to read values from</param>
public static void ReplaceDataIfNeeded(ref List<double> list, string fileName)
{
if (!File.Exists(fileName)) { return; }
var lines = File.ReadAllLines(fileName);
for (var i = 0; i < list.Count && i < lines.Length; i++)
{
if (double.TryParse(lines[i], out var dTemp))
{
list[i] = dTemp;
}
}
}
/// <summary>
/// just a helper function to translates bytes to a more readable string
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string GetHumanReadableBytes(ulong bytes)
{
var bytesReadable = string.Format("{0} bytes", bytes);
if (bytes > 1073741824)
{
bytesReadable = string.Format("{0:##.##} GB", bytes / 1073741824.0);
}
else if (bytes > 1048576)
{
bytesReadable = string.Format("{0:##.##} MB", bytes / 1048576.0);
}
else if (bytes > 1024)
{
bytesReadable = string.Format("{0:##.##} KB", (double)bytes / 1024);
}
return bytesReadable;
}
/// <summary>
/// checks to see if a string contains illegal characters for file and/or path names
/// </summary>
/// <param name="nameToValidate"></param>
/// <returns></returns>
public static bool ValidateFileAndPathNameChars(string nameToValidate)
{
var bValid = true;
var name = nameToValidate;
if (name.Trim().Length < 1) { bValid = false; }
foreach (var invalidChar in Path.GetInvalidFileNameChars())
if (name.Contains(invalidChar)) { bValid = false; }
foreach (var invalidChar in Path.GetInvalidPathChars())
if (name.Contains(invalidChar)) { bValid = false; }
if (name.Contains('.')) { bValid = false; }
return bValid;
}
/// <summary>
/// checks to see if a path contains a proper length. Full string is no greater than 260char and path is no greater than 248char
/// </summary>
/// <param name="nameToValidate"></param>
/// <returns></returns>
public static bool ValidateFileAndPathNameLength(string nameToValidate)
{
try
{
var fullPath = Path.GetFullPath(nameToValidate);
}
catch (PathTooLongException)
{
return false;
}
catch (Exception)
{
// All This function cares about is length. Ignore this exception. Dont need to log.
return true;
}
return true;
}
///
/// <summary>
/// Get useful disk usage statistics.
/// </summary>
///
/// <param name="directoryName">
/// The <see cref="string"/> directory name to be queried for usage numbers.
/// </param>
///
/// <param name="freeBytesAvailable">
/// The <see cref="ulong"/> number of free bytes available on the specified volume.
/// </param>
///
/// <param name="totalNumberOfBytes">
/// The <see cref="ulong"/> total number of bytes available on the specified volume.
/// </param>
///
/// <param name="totalNumberOfFreeBytes">
/// The <see cref="ulong"/> total number of free bytes available on the specified volume.
/// </param>
///
/// <returns>
/// <see cref="int"/> status code, determined by windows.
/// </returns>
///
[DllImport("kernel32", CharSet = CharSet.Auto)]
public static extern int GetDiskFreeSpaceEx(
string directoryName,
out ulong freeBytesAvailable,
out ulong totalNumberOfBytes,
out ulong totalNumberOfFreeBytes);
}
}