0

I have two strings:

  1. C:\folder1\folder2\folder3

  2. folder3\folder4\file1.jpg

I want to combine the two strings to give a full file path, but when using:

char[] charsToTrim = {'\\'};
var rootPathEdit = treeViewPath.TrimStart(charsToTrim);

it doesn't work, can you give me any tips?

EDIT:

Sorry I realised it wasn't very clear, I want the result of combining:

  1. C:\folder1\folder2\folder3

  2. folder3\folder4\file1.jpg

to be: C:\folder1\folder2\folder3\folder4\file1.jpg

because the two half of the file path I have overlap at the end and start.

halfer
  • 19,824
  • 17
  • 99
  • 186
James Morrish
  • 455
  • 6
  • 24
  • What does it mean "it doesn't work"? – Gilad Green Aug 02 '17 at 10:26
  • Possible duplicate of [Path.Combine absolute with relative path strings](https://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings) –  Aug 02 '17 at 10:40
  • 1
    @AndyJ In the title James said he want's to remove the "end of a string". Assuming he want#s to merge without having `folder3` two times in his path, this isn't a duplicate. But for this we need more info from James. – Mighty Badaboom Aug 02 '17 at 10:45
  • You should describe better what you are trying to accomplish. Do you always need the *parent* folder of the first path? Or you want to match several matching subfolders? – vgru Aug 02 '17 at 10:53
  • @MightyBadaboom Fair enough. –  Aug 02 '17 at 11:16

3 Answers3

3

use Path.Combine for path concatenation.

var finalPath = Path.Combine(firstPath, secondPath);

EDIT:

Since the real problem (not very clearly defined in original post) is duplicate folder, that exist in both paths, here's one way to remove extra folder:

var start = @"C:\folder1\folder2\folder3";
var end = @"folder3\folder4\file1.jpg";

var startArr = start.Split('\\');
var endArr = end.Split('\\');

var duplicateFolders = startArr.Intersect(endArr);
var final = Path.Combine(startArr.Concat(endArr.Except(duplicateFolders)).ToArray());

//... and some fix (because final path doesn't have backslash after :
final = final.Replace(":", @":\");
Nino
  • 6,931
  • 2
  • 27
  • 42
  • I didn't DV, but I assume it's because you're answering a question that should have been flagged as a duplicate. –  Aug 02 '17 at 10:40
  • @AndyJ wasn't marked as a duplicate when I proposed my asnwer :) – Nino Aug 02 '17 at 10:46
  • What if end was `var end = @"folder3\folder2\file1.jpg";`? – mjwills Aug 02 '17 at 12:31
  • @mjwills this is just an example. It is pretty bad case from the start, and if constructing path requires extra effort, OP should think of rewriting a lot of code. I don't have any intent writing more code or catching corner cases. – Nino Aug 02 '17 at 12:36
2

Don't use string operations for this because there are better ways and string operations are not as fail-safe as the code below.

Use Directory.GetParent to get the parent from the first path (cause you said in the title you want to remove the end of the first string). If you don't want to, skip this part.

var firstPath = Directory.GetParent(@"C:\folder1\folder2\folder3").ToString();
var secondPath = @"folder3\folder4\file1.jpg";

Then use Path.Combine to combine both paths.

var result = Path.Combine(firstPath, secondPath);

or when one of the paths is a relative one you should use

var finalResult = Path.GetFullPath(result);

as well.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • You should probably also include `Path.GetFullPath` for [completeness](https://stackoverflow.com/a/16646701/69809), in case one of the paths is relative. – vgru Aug 02 '17 at 10:45
  • Hi Thanks for the reply I am the issue I am having is that the two paths I have overlap. the first path has the same folder as the start of the second path. I want the result to be: C\folder1\folder2\folder3\folder4\file1.jpg using path combine I get: C\folder1\folder2\folder3\folder3\folder4\file1.jpg – James Morrish Aug 02 '17 at 10:57
  • 1
    @JamesMorrish That's why I wrote the first part: to remove the last folder from the first path ;) – Mighty Badaboom Aug 02 '17 at 11:08
1

Here is one possible avenue to explore.

Basically you split the two strings up by \ and then compare the last element of the first string vs the first element of the last string. If they are the exact same, strip one of them.

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var start = @"C:\folder1\folder2\folder3";
        var end = @"folder3\folder4\file1.jpg";

        var startArray = start.Split('\\');
        var endArray = end.Split('\\');

        var final = Path.Combine(start, end);
        var endOfStart = startArray.LastOrDefault();
        if (endOfStart  == endArray.FirstOrDefault())
        {
            final = Path.Combine(start.Substring(0, start.Length - (endOfStart ?? "").Length), end);
        }

        Console.WriteLine(final);
        Console.ReadLine();

    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63
  • Perfect, that's exactly what I wanted to achieve. Thanks a lot +1 – James Morrish Aug 02 '17 at 11:10
  • @JamesMorrish since your additional info cleared that the problem is in extra folder3, i have edited my answer with a bit different (if simpler) approach. – Nino Aug 02 '17 at 11:56