Delete file using C # Thread

I read this article ( Failed to delete the file using streams ) about my problem, but it's getting harder for me.

My problem is very simple, I just want to delete this old file, if I run the "dlMoveNovaVersao" method, the file is usually deleted, but if I put it in a stream (for example, below), I got "You do not allow". Does anyone know what the problem is? (I want to use a stream).

private void verificaVersaoSupervisor_Tick(object sender, EventArgs e) { Thread threadConexao = new Thread(threadVerificaConexao); threadConexao.Start(); } public void threadVerificaConexao() { try { Dns.GetHostEntry("www.google.com.br"); if (verificaVersao()) { try { verificaKillSupervisor(); dlMoveNovaVersao(); Application.Exit(); } catch (Exception) { } } else { Application.Exit(); } } catch (Exception) { } } public void dlMoveNovaVersao() { WebClient webClient = new WebClient(); webClient.DownloadFile("Anywebsite", @"c:\temp\supervisor.exe); try { File.Delete(@"c:\Test\supervisor.exe); //This file is always there! } catch (Exception err) { MessageBox.Show(err.Message); } 

Just describe the purpose, My program (Supervisor Starter) checks on the website if I have an old version of "Supervisor" running (using XML). If this is true, my "Supervisor Starter" checks if there is a process called "Supervisor" "start and kill after that" Supervisor Starter "download the new version and start it. (The program is small and the update does not take more than 4 seconds).

The problem starts when my "Supervisor Starter" tries to uninstall an old version of my program. If I use thread, I get "I do not have access to the file", if I use the same method in the Form class, the file will be deleted.

+6
source share
2 answers

I suspect you are using a stream while the file is in use . When a thread is running, it is running in parallel with the current thread. Did you guarantee that this file is closed ?.

Otherwise, I think a thread can be created with credentials that are not yours. But I am sure that this is not so.

See if this is different for each case.

 catch (Exception err) { MessageBox.Show("User {0}. Message {1}", System.Security.Principal.WindowsIdentity.GetCurrent().Name, err.Message); } 
+4
source

These are my functions for deleting files in streams if files are used.

 private static void Delete(System.IO.FileInfo file) { if (file.Exists) { int Attempt = 0; bool ShouldStop = false; while (!ShouldStop) { if (CanDelete(file)) { file.Delete(); ShouldStop = true; } else if (Attempt >= 3) { ShouldStop = true; } else { // wait one sec System.Threading.Thread.Sleep(1000); } Attempt++; } } } private static bool CanDelete(System.IO.FileInfo file) { try { //Just opening the file as open/create using (FileStream fs = new FileStream(file.FullName, FileMode.OpenOrCreate)) { //If required we can check for read/write by using fs.CanRead or fs.CanWrite } return false; } catch (IOException ex) { //check if message is for a File IO string __message = ex.Message.ToString(); if (__message.Contains("The process cannot access the file")) return true; else throw; } } 
0
source

Source: https://habr.com/ru/post/894509/


All Articles