Home > OS >  System.IO.File.Move() doesn't move the file
System.IO.File.Move() doesn't move the file

Time:12-20

I'm trying to make a simple exe that, when opened, moves itself to my documents folder, but when i open it, it doesn't do that, what can i do?

string fileName = "installer.exe";
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath);
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFileMove = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
System.IO.File.Move(sourceFile, destFileMove);

CodePudding user response:

File.Move(string, string) takes in 2 file names, so your second parameter is incorrect: instead of passing the target folder, you have to pass the target full file name.

If you want to preserve your previous file name, do it like this:

string destFileMove = Path.Combine( 
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    fileName);
  • Related