Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone!!!
in uploading a file sing c# desktop application , i m using
File.copy but this is used only to copying a file into another file..
and i want to copy a file into a folder or directory..so please help me in doing so...
Posted
Comments
ezrasoft 29-Mar-16 11:02am    
hi everyone
i would like to make an application that saves files such as ms word xml ppt pdf just to mention these to ms access database for a desktop computer how can i do that

C#
OpenFileDialog file = new OpenFileDialog();
            file.Multiselect = false;
            file.ShowDialog();
            txtfilename.Text = file.FileName;
 File.Copy(file.FileName,Path.Combine( @"full path of folder",Path.GetFileName(file.FileName)));
 
Share this answer
 
v2
 
Share this answer
 
// To copy a file to another location and
// overwrite the destination file if it already exists.
C#
System.IO.File.Copy(sourceFile, destFile, true);


// To move a file or folder to a new location:
C#
System.IO.File.Move(sourceFile, destinationFile);
 
Share this answer
 
Your assumption is wrong.
File.Copy can copy a file into another directory (a.k.a folder)

C#
public static void Copy(
	string sourceFileName,
	string destFileName
)


See example code:
C#
// Recursive function to copy a folder's content into another folder
void Copy(string sourceDir, string targetDir)
{
    Directory.CreateDirectory(targetDir);

    foreach(var file in Directory.GetFiles(sourceDir))
        File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

    foreach(var directory in Directory.GetDirectories(sourceDir))
        Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}


Good luck,
Edo
 
Share this answer
 
File.Copy works fine between folders and across disks: just provide the full path of the source and destination:
C#
File.Copy(@"D:\Temp\MyFile.txt", @"E:\Backups\Temporary\MyFile.txt.bak");
The only problem you may get is if the destination folder does not exist - if which case you will have to create it first.
 
Share this answer
 
Comments
Joezer BH 24-Jul-13 3:51am    
5
Try the following code

C#
public class SimpleFileCopy
{
    static void Main()
    {
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and  
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);
 
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);
 
            foreach (string s in files)
            {
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }
    }
}
.
 
Share this answer
 
v2
Comments
Joezer BH 24-Jul-13 3:51am    
5

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