9

I was trying to move a file from my Resx to my PC, but I'm keep having problems.

So I import a folder named "bad" in the Resources and I use the File.Move method to move the folder "bad" into my PC. But the program keeps crashing because it says: Cannot create a file when its already exists.

Here the code I use:

//txtpath is the root folder. I let the user choose the root folder and save it in txtpath.text

private void btnbadname_Click(object sender, EventArgs e)
{
        string source = "Resources\bad";
        string destination = txtpath.Text + @"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App";

        File.Move(source, destination);

        MessageBox.Show("脏话ID已开启, 教程请点击下面的链接");
}
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
Rain
  • 101
  • 1
  • 1
  • 8
  • I think you need to specify full path with file name for `File.Move` method. Your `source` is not a file name with full path at all. If you had `Cannot create a file when its already exists` error, I think this error comes from somewhere else.. – Soner Gönül Mar 06 '14 at 15:19
  • 2
    Why nobody uses [Path.Combine](http://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx) for path concatenation? – Alberto Solano Mar 06 '14 at 15:21
  • possible duplicate of [Cannot create a file when that file already exists when using Directory.Move](http://stackoverflow.com/questions/12667770/cannot-create-a-file-when-that-file-already-exists-when-using-directory-move) – Tony Mar 06 '14 at 15:21
  • 1
    If you want to move a folder why are you using `File.Move`, what about using `Directory.Move` – CodingMate Mar 06 '14 at 15:24
  • Are you positive your "releases" dir has trailing blank spaces `releases `? – Jason Mar 06 '14 at 15:26
  • @SonerGönül Hmm. Im not very sure how will i be able to use the resources file, is it like Properties.Resources or something? – Rain Mar 06 '14 at 15:31
  • @AlbertoSolano Hmm.. Never seem that before 0 0 – Rain Mar 06 '14 at 15:32
  • @Jason Yeah, there's no space after the "release", it just happens when I copy the code to here – Rain Mar 06 '14 at 15:32
  • @Bedo Thx, I will try using directory.move – Rain Mar 06 '14 at 15:43
  • @Bedo Thx bro, I fixed it with Directory.Move – Rain Mar 06 '14 at 15:47

4 Answers4

4

The destination Directory cannot exist. In your code you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.

See: Cannot create a file when that file already exists when using Directory.Move

Community
  • 1
  • 1
Tony
  • 16,527
  • 15
  • 80
  • 134
  • I think i get what you mean, Thank You. Is there any method I can use that allows me to move my file to a destinations and also replace if already exists? – Rain Mar 06 '14 at 15:33
  • Try using Directory.Move as Bedo said – Tony Mar 06 '14 at 17:31
4

Destination supposed to have the filename as well

string destination = txtpath.Text + @"\RADS\projects\lol_air_client\releases\0.0.1.74\deploy\assets\locale\App\yourfilename.ext";
Jude
  • 2,353
  • 10
  • 46
  • 70
1

You are using File.Move to move directory, why not using Directory.Move.

The MSDN documentation will only move files from a source to a destination, while Directory.Move will move the directory itself.

If I misunderstood you, and you want to move a file;

You can check if the file exists before or not using something like:

if(File.Exists(fileName))
    File.Delete(fileName);

Edit: If you want to iterate through the directory and make sure that the file doesn't exist before moving it, you can use something like:

//Set the location of your directories                
string sourceDirectory = @"";
string destDirectory = @"";

//Check if the directory exists, and if not create it
if (!Directory.Exists(destDirectory))
    Directory.CreateDirectory(destDirectory);

DirectoryInfo sourceDirInfo = new DirectoryInfo(sourceDirectory);
//Iterate through directory and check the existance of each file
foreach (FileInfo sourceFileInfo in sourceDirInfo.GetFiles())
{
    string fileName = sourceFileInfo.Name;
    string destFile = Path.Combine(destDirectory, fileName);
    if (File.Exists(destFile))
        File.Delete(destFile);

    //Finally move the file    
    File.Move(sourceFileInfo.FullName, destFile);
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
CodingMate
  • 333
  • 4
  • 16
  • May I ask another question? How do I write the directory if Im using files from the Resources – Rain Mar 06 '14 at 15:48
  • @user3383530 do you want to create the directory before moving the files? if so, then you can use `Directory.Exists(directoryName)` and if the directory doesn't exists, then use `Directory.CreateDirectory(directoryName)` to create that directory – CodingMate Mar 06 '14 at 15:51
  • Hmm, Do you have skype or facebook? It would be better that If i can ask you questions with instant chat lol. – Rain Mar 06 '14 at 15:55
  • So.. What im trying to do now is if user click on the button, the program will replace some files which already exists in the users pc with the files in my Resources – Rain Mar 06 '14 at 15:55
  • @rain, I've deactivated my fb account, let me check the activation – CodingMate Mar 06 '14 at 16:01
  • and for your scenario, yes, it will do that, you need to check on if the files exists before or not, and if they exists you can delete it before moving those files to the destination, in fact I will place a code in the edit, so you can iterate on the files on your source directory before moving them. Hope it would satisfy your requirements – CodingMate Mar 06 '14 at 16:03
  • Okay. Can you activate your fb or skype or something? :D because Im kinda new, i think i will bother you a lot later on lol – Rain Mar 06 '14 at 16:16
  • Alright :), will do that ma'am, just give a while as the bloody firewall prohibit me to access any social network :D, bloody fools :( – CodingMate Mar 06 '14 at 16:29
0

When using MoveTo, provide the full path of where you are sending the file, including the file name, eg, pic123.jpg. If you use DirectoryInfo to get an array of files and want to move any of them, append the Name property of the file to the directory path where you are sending the file. imgFile.MoveTo("C:\myPictures\ArchiveFolder\pic123.jpg")

Charles F
  • 31
  • 1