alice7
alice7

Reputation: 3882

move zip file using file.copy()

I am trying to move a file from server \\abc\\C$\\temp\\coll.zip to another server \\def\\c$\\temp.

I am trying to use File.Copy(source,destination). But I am getting the error in source path saying: Couldn't find the part of the path.

I am not sure what is wrong with the source path.

Upvotes: 1

Views: 8260

Answers (8)

GeeksMathGeeks
GeeksMathGeeks

Reputation: 29

To alice7,

I got same issue. And I ask it with Google Gemini, it says that one cannot directly invoke File.Copy(sourceFileName,destFileName) method to copy it from sourceFileName to destFileName. sourceFileName is considered as a uncompressed file (such as *.zip, *.7z).

For example,

Suppose that

  • D:\data folder\input folder\EventSample.zip exists file.

  • but D:\data folder\input folder\EventSample.zip is consider as an uncompress file.

  • it contains ..\content.xml file.

  • D:\data folder\input folder2\content.xml exists.

The following code snippet will throw an exception (Couldn't find the part of the path.).

using System.IO;
string sourceFileFullPath = @"D:\data folder\input folder\EventSample.zip\content.xml";
string destFileFullPath = @"D:\data folder\input folder2\content.xml";

File.Copy(sourceFileFullPath,destFileFullPath); // <- this line will throw exception

Upvotes: -1

Seb Nilsson
Seb Nilsson

Reputation: 26428

You could use a C# @ Verbatim and also use checks in the code like this:

string source = @"\\abc\C$\temp\coll.zip";
string destination = @"\\def\c$\temp\coll.zip";
string destDirectory = Path.GetDirectoryName(destination)
if (File.Exists(source) && Directory.Exists(destDirectory)) {
    File.Copy(source, destination);
}
else {
    // Throw error or alert
}

Upvotes: 5

BlueMonkMN
BlueMonkMN

Reputation: 25601

Make sure that your "\" characters are escaped if you are using C#. You have to double the backslashes or prefix the string literal with @, like this:

string fileName = @"\\abc\C$\temp\coll.zip";

or

string fileName = "\\\\abc\\C$\\temp\\coll.zip";

Upvotes: 5

Richard
Richard

Reputation: 61

Make sure you are using a valid UNC Path. UNC paths should start with \ not just . You should also consider using System.IO.File.Exists(filename); before attempting the copy so you can avoid the exception altogether and so your app can handle the missing file gracefully.

Hope this helps

Upvotes: 2

alice7
alice7

Reputation: 3882

Actually I missed @ before the two strings.The source and the destination path. That is why it was giving error.

Upvotes: 0

Wim Haanstra
Wim Haanstra

Reputation: 5998

I always use network shares for that kind of work, but UNC path's should be available too.

Don't forget that you need to escape your string when you use \'s. Also, UNC paths most of the time start with a double .

Example:

\\MyComputerName\C$\temp\temp.zip

Upvotes: 0

Jon Ownbey
Jon Ownbey

Reputation: 1448

It could be the string you are using for the path. If it is exactly as you have entered here I believe you need double backslashes. "\\" before the server name.

Upvotes: 0

Michael Meadows
Michael Meadows

Reputation: 28426

looks like you need two backslashes at the beginning:

  • \\abc\C$\temp\coll.zip
  • \\def\c$\temp

Upvotes: 3

Related Questions