43

I am programming in c# and want to copy a folder with subfolders from a flash disk to startup.

Here is my code:

private void copyBat()
{
    try
    {
        string source_dir = "E:\\Debug\\VipBat";
        string destination_dir = "C:\\Users\\pc\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";

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

        // Create subdirectory structure in destination    
        foreach (string dir in Directory.GetDirectories(source_dir, "*", System.IO.SearchOption.AllDirectories))
        {
            Directory.CreateDirectory(destination_dir + dir.Substring(source_dir.Length));          
        }

        foreach (string file_name in Directory.GetFiles(source_dir, "*.*", System.IO.SearchOption.AllDirectories))
        {
            File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message, "HATA", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}

I got an error:

Could not find a part of the path E:\Debug\VipBat

David Rogers
  • 2,601
  • 4
  • 39
  • 84
user3313131
  • 583
  • 2
  • 7
  • 9
  • 3
    You are using verbatim string literals `@""` in which case you don't need to escape out `\\` - just use a single slash. – StuartLC Feb 15 '14 at 11:13
  • @StuartLC is right. If you use _verbatim string literal_, your string will be exactly as what you write. In this case, it will be exactly E:\\Debug\\VipBat\\ which is not a valid path. – Soner Gönül Feb 15 '14 at 11:14
  • Shouldn't you fill `{0}` present in `E:\\Debug\\VipBat\\{0}`? Another thing you don't need two slashes in the path when you prefix the string with `@`. – Karthik Kalyanasundaram Feb 15 '14 at 11:20
  • Welcome to Stack Overflow! Please do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Feb 15 '14 at 11:27
  • I didn't use @ and I am sure the path is right, but I got the same error. the code is updated... – user3313131 Feb 15 '14 at 13:34
  • @user3313131: Why you have edited the question after getting the right answer? Now, Questing above doesn't match the answers below. which will confuse the new users. – Syed Uzair Uddin Apr 27 '22 at 06:14

10 Answers10

25

The path you are trying to access is not present.

string source_dir = "E:\\Debug\\VipBat\\{0}";

I'm sure that this is not the correct path. Debug folder directly in E: drive looks wrong to me. I guess there must be the project name folder directory present.

Second thing; what is {0} in your string. I am sure that it is an argument placeholder because folder name cannot contains {0} such name. So you need to use String.Format() to replace the actual value.

string source_dir = String.Format("E:\\Debug\\VipBat\\{0}",variableName);

But first check the path existence that you are trying to access.

Sachin
  • 40,216
  • 7
  • 90
  • 102
10

There's something wrong. You have written:

string source_dir = @"E:\\Debug\\VipBat\\{0}";

and the error was

Could not find a part of the path E\Debug\VCCSBat

This is not the same directory.

In your code there's a problem, you have to use:

string source_dir = @"E:\Debug\VipBat"; // remove {0} and the \\ if using @

or

string source_dir = "E:\\Debug\\VipBat"; // remove {0} and the @ if using \\
Tom Bowen
  • 8,214
  • 4
  • 22
  • 42
Akrem
  • 5,033
  • 8
  • 37
  • 64
4

Is the drive E a mapped drive? Then, it can be created by another account other than the user account. This may be the cause of the error.

Daniel B
  • 8,770
  • 5
  • 43
  • 76
ThorstenC
  • 1,264
  • 11
  • 26
  • Does this have anything to do with privileges? It's funny that I opened visual studio as an administrator and the code stopped working complaining "Could not find a part of the path". This doesn't make any sense. – Kafo Feb 26 '18 at 20:24
  • If the user who's accessing this path is an Active Directory user , then this error might be generated. – Sachintha Nayanajith Jul 23 '20 at 05:02
3

I had the same error, although in my case the problem was with the formatting of the DESTINATION path. The comments above are correct with respect to debugging the path string formatting, but there seems to be a bug in the File.Copy exception reporting where it still throws back the SOURCE path instead of the DESTINATION path. So don't forget to look here as well.

-TC

TJC
  • 91
  • 1
  • 4
3

We just had this error message occur because the full path was greater than 260 characters -- the Windows limit for a path and file name. The error message is misleading in this case, but shortening the path solved it for us, if that's an option.

RealHandy
  • 534
  • 3
  • 8
  • 27
2
File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);

This line has the error because what the code expected is the directory name + file name, not the file name.

This is the correct one

File.Copy(source_dir + file_name, destination_dir + file_name.Substring(source_dir.Length), true);
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Ryan Chong
  • 180
  • 2
  • 13
2

Probably unrelated, but consider using Path.Combine instead of destination_dir + dir.Substring(...). From the look of it, your .Substring() will leave a backlash at the beginning, but the helper classes like Path are there for a reason.

Drew Delano
  • 1,421
  • 16
  • 21
2

There can be one of the two cause for this error:

  1. Path is not correct - but it is less likely as CreateDirectory should create any path unless path itself is not valid, read invalid characters
  2. Account through which your application is running don't have rights to create directory at path location, like if you are trying to create directory on shared drive with not enough privileges etc
techExplorer
  • 810
  • 7
  • 16
  • In my case, the Security settings on a network folder I did not own did not give sufficient permissions to the service user could so it could access the file. Would have been really nice if the error message included _why_ the access failed instead of making us guess. The exception doesn't even have an inner exception to assist debugging. – Suncat2000 Feb 26 '18 at 15:20
1

I resolved a similar issue by simply restarting Visual Studio with admin rights.

The problem was because it couldn't open one project related to Sharepoint without elevated access.

0

This could also be the issue: Space in the folder name

Example: Let this be your path: string source_dir = @"E:\Debug\VipBat";

If you try accessing this location without trying to check if directory exists, and just in case the directory had a space at the end, like : "VipBat    ", instead of just "VipBat" the space at the end will not be visible when you see in the file explorer.

So make sure you got the correct folder name and dont add spaces to folder names. And a best practice is to check if folder exists before you keep the file there.

Reejesh PK
  • 658
  • 1
  • 11
  • 27