-7

How can I copy test.pdf from current folder to C:exfolder I know the siple copy way in C# But I want the code which works for any folders which the exe file in in it . I was confuised beacuse when I wanted to use current folder path visual studio 2008 erros beacaus I used :

string fileName = "test_log.LDF";
            string sourcePath = @ + Application.StartupPath;
            string targetPath = @"C:\honar2";

            // Use Path class to manipulate file and directory paths.
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
            System.IO.File.Copy(sourceFile, destFile, true);
SoheilYou
  • 907
  • 5
  • 23
  • 43
  • `Plzsendtehcodez` is not a valid question here on StackOverflow. Please try to make your questions better next time. – Jashaszun Jul 08 '15 at 18:20

2 Answers2

1

You can get directory for the exe file using the System.Environment.CurrentDirectory, then use the System.IO.File.Copy method to copy that file to any destination. Also for getting the current directory you may want to see this

Community
  • 1
  • 1
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
1

Try this

    string fileName = "test.txt";
    string sourcePath = @"C:\Users\Public\TestFolder";
    string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

    // Use Path class to manipulate file and directory paths.
    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
    string destFile = System.IO.Path.Combine(targetPath, fileName);
    System.IO.File.Copy(sourceFile, destFile, true);

For more information Please follow MSDN

PaulShovan
  • 2,140
  • 1
  • 13
  • 22