Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have folder ,subfolder and files

C:/a

C:/a/b

C:/a/c

C:/a/b/c

C:/a/c/abc.doc

I want to create this folder and files on D disk how can i do it in c#?

What I have tried:

I have tried some source on the internet but i couldnot find anything for solving this problem
Posted
Updated 19-Jul-16 2:29am

Have you tried this?

C#
Directory.CreateDirectory("C:\\newfolder");
 
Share this answer
 
C#
class Program
	{
		static void Main(string[] args)
		{
			var d = new DirectoryInfo(@"D:/temp");
			d.CreateSubdirectory("a/b/c");
			d.CreateSubdirectory("a/c");
			File.Create(@"c:/temp/a/c/abc.doc");
			Console.ReadLine();
		}
	}
 
Share this answer
 
I have created a code for you try this
:)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FolderSubfolderFile
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo din = new DirectoryInfo("C:\\a");
            SearchDirectory(din);
            Console.ReadKey();
        }

        static void SearchDirectory(DirectoryInfo din)
        {

            SearchFile(din);
            foreach (DirectoryInfo dinf in din.GetDirectories())
            {
                if (IsSystem(dinf.FullName))
                    continue;
                SearchDirectory(dinf);
            }
        }

        static void SearchFile(DirectoryInfo dinf)
        {
            foreach (FileInfo fin in dinf.GetFiles())
            {
                string newFullName = fin.FullName.Replace("C:\\", "D:\\");
                string dirs = newFullName.Remove(newFullName.LastIndexOf("\\"));
                if (!Directory.Exists(dirs))
                {
                    Directory.CreateDirectory(dirs);
                }
                File.Create(newFullName);
            }
        }
        public static bool IsSystem(string path)
        {
            FileAttributes attributes = File.GetAttributes(path);
            return (attributes & FileAttributes.System) != 0;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900