0

I wrote a program that copies all files from a certain folder to a folder on the C: drive, and it works perfectly fine on my own PC. But when I run it on another computer, it seems to only copy some of the files, and leaves the rest.

The stuff that I'm copying is in the same directory as the executable, but in a subfolder called "misc".

It collects all file directories in a string array, and uses File.Copy to copy them.

Here is the code that I have:

// Create directory
Directory.CreateDirectory("c:\\subtool\\Controller");
string dest = "c:\\subtool\\Controller\\";

// Get all files from the misc folder
string[] files = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Replace("\\subtool.exe", "") + "\\misc");

// Cycle through all files in the misc folder and copy them to destination folder "dest"
foreach (string path in files)
{
    System.IO.File.Copy(path, dest + Path.GetFileName(path), true);
}

When I run it on my main PC it runs fine, no errors. It also works when running outside of Visual Studio. However, when I run it on my laptop, it doesn't copy the files. What is this caused by?

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Have you checked the permissions of the files? Do you get any errors? – CodeLikeBeaker Jan 30 '22 at 22:01
  • 1
    Encapsulate your code in a `try-catch` block and apply some basic logging to your code. Then it would be easier for you to capture the error and apply the fix. – Rahul Sharma Jan 30 '22 at 22:04
  • I will try and apply logging to my code as suggested, thank you for replying :) – ToasterDan Jan 31 '22 at 08:25
  • let's assume that you don't have full permission on another computer which might be preventing from copying files. You can try making your application run with admin rights to perform IO operations where it requires escalated privileges. How to do that? [Check here](https://stackoverflow.com/questions/14760074/file-access-denied-changing-permissions) – Abhinav Pandey Jan 31 '22 at 18:52

0 Answers0