C# Program to split parts in a Windows directory


Firstly, set a string i.e. your Windows directory path −

string str = @"D:\Downloads\Amit";

Now use the Split() method and split wherever the \ occur −

str.Split(' \')

The following is the complete code −

Example

 Live Demo

using System;
class Program {
   static void Main() {
      string str = @"D:\Downloads\Amit";
      Console.WriteLine("Directory...
"+str);       string[] myStr = str.Split('\');       Console.WriteLine("
Split...");       foreach (string ch in myStr) {          Console.WriteLine(ch);       }    } }

Output

Directory...
D:\Downloads\Amit

Split...
D:
Downloads
Amit

Updated on: 22-Jun-2020

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements