.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Create and Copy Folder information in C#.Net

Posted By:Narayanan       Posted Date: November 25, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

In this article, we learn create a folder a based on user Input in the Windows Application In the C#.net
 

Step by step Create a Folder and Copy the Files into another Folder.

Step:1

    Create a New Project and Create a Form in that Project.

Step:2

 Create a TextBox, 3 button in the Form.

textBox  name: txtNo
Button Name :btn_Create,Text Name :Create
Button Name :btn_Copy,Text Name :Copy
Button Name :btn_Close,Text Name :Close

Step: Coding Part

Declaration Part
 
string[] InputPath;
        string[] OutputPath;
        string InputDir = @"D:\InTesting";
        string OutputDir = @"E:\outTesting";

step:3
  
private void btn_Create_Click(object sender, EventArgs e)
        {
            try
            {
                #region Get a value from textbox into integer
                int no = Convert.ToInt32(textBox1.Text);
                #endregion Get a value from textbox into integer
                InputPath = new string[no];
                OutputPath = new string[no];
                #region Create folders based on Input
                for (int idx = 0; idx < no; idx++)
                {
                    InputPath[idx] = @"D:\InTesting\COM-" + idx.ToString();
                    OutputPath[idx] = @"E:\outTesting\COM-" + idx.ToString();

                    #region Create a Directory in D Drive
                    if (!Directory.Exists(InputPath[idx]))
                    {
                        Directory.CreateDirectory(InputPath[idx]);
                    }
                    #endregion Create a Directory in D Drive
                    #region Create a Directory in E Drive
                    if (!Directory.Exists(OutputPath[idx]))
                    {
                        Directory.CreateDirectory(OutputPath[idx]);
                    }
                    #endregion Create a Directory in E Drive
                

                }
                #region Transfer File from One Path to Another Path
                String In_Path = @"D:\IMG0001A.jpg";
                String In_Path1 = @"D:\Difference in asp.net.txt";
                File.Copy(In_Path1, InputPath[0] + "\\" + "Difference in asp.net.txt");
                File.Copy(In_Path, InputPath[1] + "\\" + "IMG0001A.jpg");
                #endregion Transfer File from One Path to Another Path
                #endregion Create folders based on Input
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


Step:4
  
private voidbtn_Copy_Click(object sender, EventArgs e)
        {
            #region Counting of Files and Folders in a Directory
            System.IO.DirectoryInfo InputDirInfo = new DirectoryInfo(InputDir);
            System.IO.DirectoryInfo OutputDirInfo = new DirectoryInfo(OutputDir);
            #endregion Counting of Files and Folders in a Directory
            #region Declaration 
            int Infolders;
            int Outfolders;
            #endregion Declaration
            #region Getting Directory Count
            Infolders = InputDirInfo.GetDirectories().Length;
            Outfolders = OutputDirInfo.GetDirectories().Length;
            #endregion Getting Directory Count
            #region Getting Files Count
            
            #endregion Getting Files Count

            #region Processing
            if (Infolders == Outfolders)
            {
    
    InputPath = new string[Infolders];
    OutputPath = new string[Infolders];
    for (int idx = 0; idx < Infolders; idx++)
    {
        InputPath[idx] = @"D:\InTesting\COM-" + idx.ToString();
        OutputPath[idx] = @"E:\outTesting\COM-" + idx.ToString();
        string[] GetFoldername = InputPath[idx].Split('\\');
        string FolderName = GetFoldername[2];
        string[] SplitFolderName = FolderName.Split('-');
        string SplitValue = SplitFolderName[1];
        System.IO.DirectoryInfo Input_Direectory = new DirectoryInfo(InputPath[idx]);
        String[] SourceDirectory = Directory.GetFiles(InputPath[idx]);
        
        foreach (string SubDir in SourceDirectory)
        {
            string[] GetFileName = SubDir.Split('\\');
            string SourceFileName = GetFileName[3];
            File.Copy(InputPath[idx] + "\\" + SourceFileName, OutputPath[idx] + "\\" + SourceFileName, true);
       }
    }
            }
            #endregion Processing


        }

Step:5
private void btn_Close_Click(object sender, EventArgs e)
{
            
    Application.Exit();

}

Step:6

  Run the Application


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend