can you please give me a hand on this?
I have a .dll file say in the folder on my desktop.
I need to copy it to c:\Program Files(86)\Program Folder\ directory
I've already tried to do "File.Copy" but I can't figure out how to make it work with spaces in the folder names. Tried to put the full address in quotes - like "\"my folder\myfile.dll\"" but it also doesn't work.
Tried to write a bash file - but can't figure out how to run it through C#.
Pls help. Any solution is appreciated.
I also need to "Force Overwrite" if possible.

- 891
- 1
- 11
- 17
-
You shouldn't need the quotes in C#. Put the File.Copy in a try/catch block and see what the error is. – 001 Sep 07 '12 at 18:36
-
See documentation (includes example): http://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.100).aspx – GameScripting Sep 07 '12 at 18:37
2 Answers
Try:
File.Copy(@"c:\Program Files(86)\Program Folder\mydll.dll", @"C:\mydll.dll");
To force overwrite simply use:
File.Copy(@"c:\Program Files(86)\Program Folder\mydll.dll", @"C:\mydll.dll",true);
as per MSDN docs:
public static void Copy(
string sourceFileName,
string destFileName,
bool overwrite
)
Parameters
sourceFileName Type: System.String The file to copy.
destFileName Type: System.String The name of the destination file. This cannot be a directory.
overwrite Type: System.Boolean true if the destination file can be overwritten; otherwise, false.
As noted in another answer you may not have appropriate permissions you can try checking this by:
string directoryPath = @"c:\Program Files(86)\Program Folder";
bool isWriteAccess = false;
try {
AuthorizationRuleCollection collection = Directory.GetAccessControl (directoryPath).GetAccessRules (true, true, typeof (System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in collection) {
if (rule.AccessControlType == AccessControlType.Allow) {
isWriteAccess = true;
break;
}
}
} catch (UnauthorizedAccessException ex) {
isWriteAccess = false;
} catch (Exception ex) {
isWriteAccess = false;
}
if (!isWriteAccess) //we cant write to location
{
//handle notifications
} else { //we can write to the location
}

- 4,546
- 3
- 40
- 69

- 36,155
- 13
- 81
- 138
-
the second `catch` block is doing the same as the first one. (probably a typo?) @DavidKroukamp – A-Sharabiani Jun 18 '15 at 19:38
Space in the path is no problem.
But did you take into account you need admin priviliges to copy the file? That means you need a manifest file on the application
Also please post the actual exception, and the code. Atm we can only guess