user2272050
user2272050

Reputation: 1

Changing Directory Paths in C#

Beginner here.

I have a directory structure that has a folder with a random number in the path.

i.e. c:\folderA\subfolder\dump1\history\var, c:\folderA\subfolder\dump22\history\var

I want the program to read each directory path dynamically. So I split the paths into "partial path" + "fix path". See code.

This is done because \dump* is the unknown and I don't want to hard code it.

The snippet of code below works and is successful but I want to manipulate a portion of the string from the result.

And there lies my problem, I don't know how to change partition string c:\ to something else. Perhaps a new partition d:\ or a UNC path \\someserver\fileshare

If successfull, it would look like this:

D:\folderA\subfolder\dump22\history\var

Or

\\someserver\fileshare\folderA\subfolder\dump22\history\var

This is done so I can write additional code to move contents from \var to another directory etc.

{
    static void Main()
    {

        // Grab directory root

        string[] array1 = Directory.GetDirectories(@"c:\folderA\subfolder\");


        // Display all directory paths
        Console.WriteLine("--- Paths---");
        foreach (string name in array1)
        {
             // Add Partial Path + Fix Path

            String CombinePath = (name + "\\history\\var");
            Console.WriteLine(CombinePath);

            //Results
    //c:\folderA\subfolder\dump1\history\var
            //c:\folderA\subfolder\dump22\history\var

            {

                //pause
                Console.ReadLine();
            }
        }
    }
  }
}

Upvotes: 0

Views: 5626

Answers (3)

Tianyun Ling
Tianyun Ling

Reputation: 1097

CombinePath.Replace(@"C:\", @"D:\");

Upvotes: 2

cat916
cat916

Reputation: 1361

You can define a dictionary data structure to hold string. If you don't want to define your own string path generation, you could have a look QueryString

Upvotes: 0

FastGeek
FastGeek

Reputation: 411

You can do a search and replace using regular expressions

Upvotes: 0

Related Questions