BillWoodruff
Я использую это для построения путей к файлам. Он используется вот так:
var pathtest = ".xslx".GetFilePath("C:", "how", "c", "s", "ex");
using System;
using System.IO;
using System.Text;
namespace YourNameSpace
{
public static class FileExtensions
{
private static StringBuilder sb = new StringBuilder();
private static char pathdelimiter = '\\';
public static string GetFilePath(this string extension, params string[] pathElements)
{
sb.Clear();
string path = null;
int last = pathElements.Length - 1;
for (int i = 0; i < last; i++)
{
sb.Append(pathElements[i]);
sb.Append(pathdelimiter);
}
sb.Append(pathElements[last]);
sb.Append(extension);
try
{
path = Path.GetFullPath(sb.ToString());
}
catch (Exception e)
{
switch (e.GetType().Name)
{
case "NotSupportedException":
throw new ArgumentException(@"extension or path elements misuse a colon", e.InnerException);
case "ArgumentNullException":
throw new ArgumentNullException(@"extension or path elements are null", e.InnerException);
case "InvalidCastException":
throw new ArgumentException(@"extension or path elements contain invalid characters", e.InnerException);
case "SecurityException":
throw new ArgumentException(@"No access to that location", e.InnerException);
case "PathTooLongException":
throw new ArgumentException(@"path is too longn", e.InnerException);
}
}
return path;
}
}
}