user1306322
user1306322

Reputation: 8721

How to replace part of the path?

I'm making a batch processing application that should convert files from directory A and place them into directory B preserving the same path after the relative root directory. To do that I need some way to convert the input file path into output file path.

Example:

before: C:\MyProject\Files\Input_\file1.cs
after:  C:\MyProject\Files\Output\file1.cs
        ———————————————————       ————————
          ↑ ↓ these remain unchanged ↑ ↓
        ———————————————————       —————————————————————————
before: C:\MyProject\Files\Input_\folder\subfolder\file2.cs
after:  C:\MyProject\Files\Output\folder\subfolder\file2.cs

I hope it's clear what I'm after. Is there a standard method in System.IO namespace that can do this? If not, how do I implement it?

Upvotes: 0

Views: 8461

Answers (3)

Artem Tsetkhalin
Artem Tsetkhalin

Reputation: 249

You can use String.Replace for path's strings.

Upvotes: 0

Antony Benito
Antony Benito

Reputation: 84

String.Replace("Input_","Output");

Get the path and assign to a string variable and do a replace.

Upvotes: 4

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

Path class may help (at least in splitting file name from path and combining back), but you may need to do some string manipulations yourself too to replace particular component of the path.

Upvotes: 0

Related Questions