Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am trying to get my program copied on run to %appdata%\master folder. This is my code.
XML
// this is for making a directory
string a =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\Master\\";
            DirectoryInfo di = Directory.CreateDirectory(a);
// this is for getting exe path
            string src = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// this is where I want to copy the exe
            string dest = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\Master\\";
            File.Copy(src, dest);

I get this error:

Quote:

System.IO.DirectoryNotFoundException was unhandled
Message: An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
Additional information: Could not find a part of the path 'C:\Users\ShomilaC\AppData\Roaming\Master\'.


Thanks in advance
Posted

1 solution

When you copy a file into another directory, you also have to specify a file name; right now you are copying to %AppData%\Master, but you have to copy to %AppData%\Master\FileNameHere.exe. Use this File.Copy call to copy the executable correctly:
C#
File.Copy(src, Path.Combine(dest, Path.GetFileName(src)));
 
Share this answer
 
Comments
Wendelius 27-Sep-15 14:11pm    
Exactly, 5'd
BillWoodruff 27-Sep-15 22:51pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900