Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
I want to move all files into a directory which should be created at runtime

For Example.

C:\users\Desktop\Sample


Its has some files such as

file1
file2
file3
file4


Here what I need to do is, I want to create a directory where all files are stores
That is C:\users\Desktop\Sample


I want to create a directory named Temp.

Now I want to move all the files into the Temp directory.

Can anyone get me the code
Posted
Updated 12-Feb-14 2:10am
v2

try this simple code:-
string[] fileArray = Directory.GetFiles(@"c:\Dir\");

or
C#
class Program
{
    static void Main()
    {
    // Put all file names in root directory into array.
    string[] array1 = Directory.GetFiles(@"C:\");

   
    // Display all files.
    Console.WriteLine("--- Files: ---");
    foreach (string name in array1)
    {
        Console.WriteLine(name);
    }  
   
}


refer:-
http://www.dotnetperls.com/directory-getfiles[^]
http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx[^]
 
Share this answer
 
include
C#
using System;
using System.IO;


string[] files = Directory.GetFiles(@"C:\");
C#
foreach (string name in files)
    {
        Console.WriteLine(name);
    }
 
Share this answer
 
C#
string[] array1 = Directory.GetFiles(@"C:\");
foreach (string name in array1)
    {
        Console.WriteLine(name);
    }


Regards,

Praveen
 
Share this answer
 
PLease refer following code:-

using System.IO;

DirectoryInfo objdir = new DirectoryInfo(@"D:\");//directory path
           FileInfo [] objF= objdir.GetFiles();
           foreach (FileInfo F in objF)
           {
               F.Name.Trim();//file name
           }
 
Share this answer
 
The code below works fine
C#
String directoryName = @"C:\Users\XXXXXX\Desktop\Refrigeration\RCA\RCA001\Children";
            DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
            if (dirInfo.Exists == false)
            {
                Directory.CreateDirectory(directoryName);
                MessageBox.Show("Created");
            }
            //else
            //{
            //    Directory.Delete(directoryName);
            //    MessageBox.Show("Deleted");
            //}



            List<string> MyFiles = Directory
                   .GetFiles(@"C:\Users\XXXXXX\Desktop\Refrigeration\RCA\RCA001","*.*",SearchOption.AllDirectories).ToList();

            foreach (string file in MyFiles)
            {
                FileInfo mFile = new FileInfo(file);
               
                //if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
                    mFile.MoveTo(dirInfo + "\\" + mFile.Name);
            }

            MessageBox.Show("Success");</string>
 
Share this answer
 

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