116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// File path trunk
|
|
/// </summary>
|
|
/// <param name="dirPath"> ex. c:\mainDir\subDir\text.text</param>
|
|
/// <param name="trunkLength"> ex. 12 char </param>
|
|
/// <returns> ex. will return "...\subDir\text.text" because "subDir\text.text" is > 12 </returns>
|
|
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 "?";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create the directory path
|
|
/// </summary>
|
|
/// <param name="dirPath"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create the directory for a file path
|
|
/// </summary>
|
|
/// <param name="pathForFile"></param>
|
|
/// <returns>return true on error</returns>
|
|
public static bool CreateDirPathForFile(string pathForFile)
|
|
{
|
|
try
|
|
{
|
|
return CreateDirPath(GetDirPath(pathForFile));
|
|
}
|
|
catch
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Combine a directory path and sub path to a compleate path
|
|
/// </summary>
|
|
/// <param name="dirPath"></param>
|
|
/// <param name="subFilePath"></param>
|
|
/// <returns></returns>
|
|
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());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get directory path
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns>return true on error</returns>
|
|
public static string GetDirPath(string filePath)
|
|
{
|
|
return filePath.Remove(filePath.Trim().LastIndexOf("\\"));
|
|
}
|
|
}
|
|
}
|