using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XYLEM.Base.Files
{
internal class PathTools
{
///
/// File path trunk
///
/// ex. c:\mainDir\subDir\text.text
/// ex. 12 char
/// ex. will return "...\subDir\text.text" because "subDir\text.text" is > 12
public static string FilePathStartTrunk(string dirPath, int trunkLength)
{
try
{
if (!String.IsNullOrEmpty(dirPath) && (dirPath.Length > 40))
{ // if to big .. then trunk it
var pathSplits = dirPath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (pathSplits.Length > 0)
{ // are able to trunk
dirPath = "";
foreach (var item in pathSplits.Reverse())
{
dirPath = CombineDirAndFileSubPath(item, dirPath);
if (dirPath.Length > 40)
{
dirPath = CombineDirAndFileSubPath("...", dirPath);
break; // max length is now then just exit loop
}
}
}
}
return dirPath;
}
catch (Exception ex)
{
AppConst.Logger.AddFuncLog(true, "PathTools", "FilePathStartTrunk()", ex);
}
return "?";
}
///
/// Create the directory path
///
///
///
public static Boolean CreateDirPath(string dirPath)
{
try
{
// Determine whether the directory exists.
if (Directory.Exists(dirPath))
{
//That path exists already
return false;
}
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(dirPath);
return false;
}
catch
{
return true;
}
}
///
/// Create the directory for a file path
///
///
/// return true on error
public static bool CreateDirPathForFile(string pathForFile)
{
try
{
return CreateDirPath(GetDirPath(pathForFile));
}
catch
{
return true;
}
}
///
/// Combine a directory path and sub path to a compleate path
///
///
///
///
public static string CombineDirAndFileSubPath(string dirPath, string subFilePath)
{
dirPath = dirPath.Trim().EndsWith("\\") ?
dirPath.Trim().Remove(dirPath.LastIndexOf("\\")) :
dirPath.Trim();
return Path.Combine(dirPath, subFilePath.Trim());
}
///
/// Get directory path
///
///
/// return true on error
public static string GetDirPath(string filePath)
{
return filePath.Remove(filePath.Trim().LastIndexOf("\\"));
}
}
}