/*
* 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
{
///
/// A collection of handy disk-related methods.
///
public class DiskUtility : Exceptional
{
///
/// 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
///
/// list to substitute values into, this as a ref to make it obvious it will be modified
/// file to read values from
public static void ReplaceDataIfNeeded(ref List 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;
}
}
}
///
/// just a helper function to translates bytes to a more readable string
///
///
///
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;
}
///
/// checks to see if a string contains illegal characters for file and/or path names
///
///
///
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;
}
///
/// checks to see if a path contains a proper length. Full string is no greater than 260char and path is no greater than 248char
///
///
///
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;
}
///
///
/// Get useful disk usage statistics.
///
///
///
/// The directory name to be queried for usage numbers.
///
///
///
/// The number of free bytes available on the specified volume.
///
///
///
/// The total number of bytes available on the specified volume.
///
///
///
/// The total number of free bytes available on the specified volume.
///
///
///
/// status code, determined by windows.
///
///
[DllImport("kernel32", CharSet = CharSet.Auto)]
public static extern int GetDiskFreeSpaceEx(
string directoryName,
out ulong freeBytesAvailable,
out ulong totalNumberOfBytes,
out ulong totalNumberOfFreeBytes);
}
}