15

I am writing an application in C# which will be compiled and run under Windows but which will, in part, be responsible for uploading files to a folder structure on Linux servers. In my Windows application I would like to be able to easily combine Linux directories and file names together with Path.Combine. Is there a way to temporarily override Path.Combine with a different path separator?

3
  • 3
    You could just wrap the Path.Combine() call and add a Replace to the result. Commented Sep 26, 2014 at 19:49
  • 2
    You know that there are more differences between Windows and Linux paths than just the separator character, right? Commented Sep 26, 2014 at 20:00
  • For local paths windows will accept both / and \. This wont work for network paths of course. Commented Sep 26, 2014 at 20:02

2 Answers 2

6

If you only want combine, there is my solution

public static string PathCombine(string pathBase, char separator = '/', params string[] paths)
{
    if (paths == null || !paths.Any())
        return pathBase;

    #region Remove path end slash
    var slash = new[] { '/', '\\' };
    Action<StringBuilder> removeLastSlash = null;
    removeLastSlash = (sb) =>
    {
        if (sb.Length == 0) return;
        if (!slash.Contains(sb[sb.Length - 1])) return;
        sb.Remove(sb.Length - 1, 1);
        removeLastSlash(sb);
    };
    #endregion Remove path end slash

    #region Combine
    var pathSb = new StringBuilder();
    pathSb.Append(pathBase);
    removeLastSlash(pathSb);
    foreach (var path in paths)
    {
        pathSb.Append(separator);
        pathSb.Append(path);
        removeLastSlash(pathSb);
    }
    #endregion Combine

    #region Append slash if last path contains
    if (slash.Contains(paths.Last().Last()))
        pathSb.Append(separator);
    #endregion Append slash if last path contains

    return pathSb.ToString();
}

With this, you can call

PathCombine("/path", paths: new[]{"to", "file.txt"});
// return "/path/to/file.txt"
PathCombine(@"\\path", '\\', "to", "file.txt");
// return @"\\path\to\file.txt"
PathCombine("/some/bin:paths/bin", ':', "/another/path", "/more/path");
// return "/some/bin:paths/bin:/another/path:/more/path"
Sign up to request clarification or add additional context in comments.

1 Comment

This is quite a lot of code. If you have a lot of situations where you need this, it could be worthwhile. If not, I would be tempted to just use Path.DirectorySeparatorChar
3

What you should do is create samba share directories.

This way you can just access it like a windows network path.

var path = @"\\"+linuxHostname + @"\sambaShare\";

But to answer your question you cannot change the Path.Combine slash .. maybe a string replace would do ?

var linuxPath = winPath.Replace('\\','/');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.