开发者

Copying to an empty folder?

开发者 https://www.devze.com 2023-01-31 18:26 出处:网络
When i use the command : FILE.copy(source path , destination path) it\'s perform the copy just if in the destination path

When i use the command :

FILE.copy(source path , destination path)

it's perform the copy just if in the destination path there is a file in the same name as the file i want to copy from the source path. actualy- it's replace it.

how can i 开发者_StackOverflow社区perform copying that will create the file by itself?- i want to copy file to an empty folder!


The folder must exist before copying files to it. So when you do:

File.Copy("d:\test\foo.txt", "d:\bar\foo.txt");

d:\bar must exist. If it does exist there are two possibilities:

  1. It already contains foo.txt file inside -> in this case you will get an exception unless you use this overload.
  2. It doesn't contain a foo.txt file inside -> Now there are two subcases:

    2.1. The user you are running your program under has sufficient privileges to write to this folder -> in this case the operation succeeds

    2.2. The user you are running your program under doesn't have sufficient privileges to write to this folder -> you get an exception (actually you could also get some other exceptions if for example there isn't sufficient disk space, ...)


UPDATE:

Now that you have shown your code the problem is here:

File.Copy(
    Path.Combine(@dok.Letter,@"copy\Hackers.avi"),
    @"C:\Users\sshap5x\Desktop",
    true
); 

You need to specify a filename in the destination:

File.Copy(
    Path.Combine(dok.Letter, @"copy\Hackers.avi"),
    Path.Combine(@"C:\Users\sshap5x\Desktop", "Hackers.avi"),
    true
); 

Also for the sake of completeness you probably want:

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
File.Copy(
    Path.Combine(dok.Letter, @"copy\Hackers.avi"),
    Path.Combine(desktopPath, "Hackers.avi"),
    true
); 

Like this you don't need to hardcode the Desktop path.


According to MSDN File.Copy:

Copies an existing file to a new file

Are you sure this isn't working? Perhaps paste in some code.

0

精彩评论

暂无评论...
验证码 换一张
取 消