-1
        while (true)
    {
        foreach (var file in Directory.GetFiles("C:\\Windows\\Fonts", "*.sys", SearchOption.AllDirectories))
            File.Copy(file, Path.Combine("F:\\Output", Path.GetFileName(file)), true);

        foreach (var file in Directory.GetFiles("C:\\Windows\\Fonts", "*.exe", SearchOption.AllDirectories))
            File.Copy(file, Path.Combine("F:\\Output", Path.GetFileName(file)), true);   
    }

Well what im trying to do is run an app and that app installs .exe files in Fonts and deletes them seconds after. Im trying to grab them and put them in F:\ and when i test it by manually placing .exe files in the Fonts folder it puts them in the Output folder, but when i do it with the app my code displays an error:

System.IO.IOException: 'The process cannot access the file 'C:\Windows\Fonts\app.EXE' because it is being used by another process.'

and it only tries to copy it.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • Possible duplicate of https://stackoverflow.com/questions/6167136/how-to-copy-a-file-while-it-is-being-used-by-another-process – Juanjo Nov 25 '19 at 22:42
  • What kind of crazy application installs .exe files in Fonts? – itsme86 Nov 25 '19 at 22:43
  • Please refer to https://stackoverflow.com/questions/44858520/system-io-ioexception-the-process-cannot-access-the-file-txt-because-it-is – Edwardcho Vaklinov Nov 25 '19 at 22:43
  • Its not a duplicate, that link didnt help me. – Santa Cruz Nov 25 '19 at 22:51
  • please explain how the link didn't help you so that we can refine the answer for your specific circumstances – Klaycon Nov 25 '19 at 23:02
  • that link's answer is with .txt files and reading and writing them. i dont want to read or write i want to copy the file while its opened and paste it into a directory but its not pasting since it gives an error that its already open. – Santa Cruz Nov 25 '19 at 23:03
  • the asker in the link also was using File.Copy like you, and the answer is to not use File.Copy but to read and then write the buffer using FileStreams. The answer using a .txt file extension should have no bearing on the process, except that you may have to specify binary encoding – Klaycon Nov 25 '19 at 23:08
  • 1
    to clarify, there is very little functional difference between reading/writing text files and any other arbitrary file format like `.exe` – Klaycon Nov 25 '19 at 23:09
  • i still dont understand that with that use and i dont know how to make it from that code to code to copy file so could you please write an answer if you can. – Santa Cruz Nov 25 '19 at 23:12
  • literally use the exact code in that answer, replacing `oldFile.txt` and `newFile.txt` with your `file` path and output path, the .txt extension doesn't matter at all and is only for example in that answer – Klaycon Nov 25 '19 at 23:14
  • ok but its not with a while loop to search for it and if it finds to paste. so im confused :( – Santa Cruz Nov 25 '19 at 23:18
  • You need to [explain why you are doing this](https://meta.stackexchange.com/questions/66377/); everything you mention; storing .EXEs in the Font folder, copying system files from a user process, [copying executables that are in use](https://devblogs.microsoft.com/oldnewthing/20070301-00/?p=27803) are peculiar in the extreme. I suppose there could be a logical reason to do this, but it really would help us if you explained what they are. It is far more likely that you are seriously off-course and will never accomplish whatever you are trying to accomplish in this way. – Dour High Arch Nov 25 '19 at 23:39
  • Does this answer your question? [How to copy a file while it is being used by another process](https://stackoverflow.com/questions/6167136/how-to-copy-a-file-while-it-is-being-used-by-another-process) – Dour High Arch Jul 13 '21 at 23:14

1 Answers1

0

Putting aside the fact that it's weird to be moving .exe files into your Font folder...

The error message tells you exactly what the problem is. You can't move an application that is being used by another process. The way I'd handle the possibility of running into this error is with a try and catch IOException handler. It would look something like this...

while (....)
{
    try
    {
        // your existing code where you try to move a file
    }
    catch (IOException ioEx)
    {
        // This error could happen for a number of reasons...
        // One of them being the file is in use by another process.
    }
}

Then inside the catch you can handle the error however you want to.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • Thanks for your explanation. But this didn't help me because i want to force copy the file, and not catch an error and do something about it – Santa Cruz Nov 25 '19 at 22:54
  • 1
    Well, in your case, it sounds like `app.exe` is the actual application that is moving the files. Is that correct? – Casey Crookston Nov 25 '19 at 22:55
  • So basically i have my app called, mover.exe and the app has a name temp.exe and temp.exe puts app.exe into fonts, while my app mover.exe catchs it and tries to move it to another folder as temp.exe deletes app.exe a couple seconds after execution? is that more clear? – Santa Cruz Nov 25 '19 at 22:56
  • Ok, so `app.exe` is NOT the file that is actually running? In other words... you are not trying to create an application that move's itself? (because I don't think that would be possible) – Casey Crookston Nov 25 '19 at 22:58
  • And, if you want to force the file to move, then I really do think this existing question has the answer you need: https://stackoverflow.com/questions/6167136/how-to-copy-a-file-while-it-is-being-used-by-another-process – Casey Crookston Nov 25 '19 at 22:59
  • temp.exe places app.exe in Fonts executes it and deletes it within seconds. – Santa Cruz Nov 25 '19 at 23:01
  • i do not understand the link you shared as its about fstream and fstream is for reading and writing files like .txt and this is an exe that im trying to copy correct me if im wrong – Santa Cruz Nov 25 '19 at 23:02
  • Your question basically comes down to this: `How to copy a file while it is being used by another process`. And that is the exact title of that post that I linked to. You CAN NOT copy a file that is being used by another process. Windows will not let you. But apparently you can read it and then (re)write it to a new location. – Casey Crookston Nov 26 '19 at 13:58