4
I’m developing an integrated system with a TEF. Every time a credit card transaction is performed the TEF creates a file with a specified name in a specific directory, my application reads this file and performs the download and prints the receipt.
Happens at random times the system gives me an exception:
System.IO.Ioexception: Process cannot access file 'C: XXXXXX xxxx.xxx' because it is being used by another process.
I’m using the API Restart Manager
to check if the file is already in use, but when identifying that yes, as who is using the file is the application itself, I can not do processo.Kill()
because it will close my own application.
Always after I use the file I’m putting arquivo.close()
.
I don’t understand why you’re still running.
try {
StreamReader sr = new StreamReader(e.FullPath);
line = sr.ReadLine();
while (line != null)
{
//Código da para armazenas as informações importantes em variáveis
line = sr.ReadLine();
}
sr.Close();
} catch (Exception a) {
// mensagem da exceção
}
I can see which process is using the same file I’m trying to read, I did based on this answer: How to know if the file is being used by another process before trying to read
I need to end this process and/or close the file that was opened by the other process, without closing the application
It may be open and being accessed by another process.
– Marconi
can post part of the code to analyze ?
– Hiago Souza
Yes it certainly is, but the other process is of my own application, which somehow did not finish, how can I force this process to be finalized without closing my application?
– jonathan
@Jonathan Use the button Edit below your question to include new information /codes to improve your question. Comments are not a good place for this. By the way, welcome to SOPT!
– emanuelsn
Thanks for the tip
– jonathan
I also recommend: [ASK] and [TOUR]. These pages contain some recommendations to ask good questions.
– emanuelsn
Ultilize the Finally. Try{ tries,... } catch (Exception e) { //catches the error } Finally { apos everything and before the sr.Close Return(); }
– Hiago Souza
I changed sr.close() to just before Return and includes Finally, I will wait because the error occurs after a large number of transactions, if it works I will let you know here.
– jonathan
It wouldn’t be the case to write your app as a service?
– Leonel Sanches da Silva
Unfortunately even changing the close() to before the place Return and inserting the Finally, the problem still persists.
– jonathan
@Jonathan You create this, archive and soon after read? will it not be that it is not getting open in the creation itself?
– Andrew Alex
who creates the file and a second application, which controls the TEF, I do a read in the directory, and I wait for the creation of the file when the file is created with the name I am waiting for, I read it
– jonathan
@Your application Jonathan is windows form or web, or neither of the two ? (:
– Thiago Friedman