Showing posts sorted by relevance for query PathCombine. Sort by date Show all posts
Showing posts sorted by relevance for query PathCombine. Sort by date Show all posts

November 2, 2010

Deleting a Read-Only File

See also the FileAttributesExtender class
and the PathCombine class
string fullPath = PathCombine(pathRoot, relativePath);
if (File.Exists(fullPath))
{
    FileAttributes fas = File.GetAttributes(fullPath);
    if (fas.Contains(FileAttributes.ReadOnly))
        File.SetAttributes(fullPath, FileAttributes.Normal);
    File.Delete(fullPath);
}

October 13, 2010

Path.Combine

See here for a description of the behaviour of this method.
Following method resolves some weaknesses in the static method "Path.Combine" found in the System.IO namespace.
public static string PathCombine(string rootPath, string relativePath)
{
    Debug.Assert(rootPath != null);
    Debug.Assert(relativePath != null);

    string res = string.Empty;
    if (rootPath.EndsWith(":"))
    {
        rootPath += @"\";
    }
    // remove initial single '\' or '/' from relative path
    if (relativePath.StartsWith(@"\") || 
        relativePath.StartsWith(@"/"))
    {
        if (! (relativePath.StartsWith(@"\\") || 
               relativePath.StartsWith(@"//")))
        {
           relativePath = relativePath.Substring(1); 
        }
    }
    res = Path.Combine(rootPath, relativePath);
    res = res.Replace('/', '\\'); // replace '/' with '\'
    return res;
}