Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file using C# Thread

Tags:

c#

I was reading this article (Can't delete a file using threads) about my problem but things are getting difficult to me.

My problem is really simple, I just want to delete this old file, if I start the method "dlMoveNovaVersao" normally the file is deleted but if I put this on a thread (like bellow) I got "You are not allow". Someone knows what's the problem? (I wanna use thread).

    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 discribe the purpose, My program (Supervisor Starter) check on website if I have an old version of "Supervisor" running (using XML), If it's true my "Supervisor Starter" verify if there is a process called "Supervisor" running and kill it after that "Supervisor Starter" download the new version and run it. (The program is small and the update don't take more then 4 seconds).

The problem start when my "Supervisor Starter" try delete the old version of my program. If I use thread I receive "I haven't permission to access the file", if I use the same method on Form class the file is deleted.

like image 482
Carvrodrigo Avatar asked Aug 08 '11 02:08

Carvrodrigo


People also ask

What is remove () in C?

The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value. #include<stdio.h> int main() {

How do I delete files from command line?

Use the delete command After reaching the desired folder, use the del command, followed by the file name. If you receive a prompt to delete the file, type 'Y' and press 'Enter'.

How do I force delete a file in C drive?

Press the Windows key + R and type cmd to open the Command Prompt or just search for Command Prompt at the start. Click the "Run as administrator". Step 2. In the Command Prompt, enter del and location of folder or file you want to delete, and press "Enter" (for example del c:\users\JohnDoe\Desktop\text.

Is delete a keyword in C?

The delete keyword replaces the free function in C and will release storage reserved with new. int *ptr1; // Declare a pointer to int.


1 Answers

I suspect that you're running the thread while the file is in use. When the thread runs, it runs in parallel with the current thread. Have you ensured that that file is closed?.

Otherwise I think that the thread maybe being created with a credentials that are not yours. But I'm pretty sure this is not the case.

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);
    }
like image 58
Preet Sangha Avatar answered Oct 25 '22 02:10

Preet Sangha