how to replace a file when they stop using? C #

0

I am making an application that shows some pdfs and has two accesses, one the user that views them and another the administrator that updates the pdf. The problem is that updating the files will give an error if another person is viewing them.

replace code.

var dlg = new OpenFileDialog { DefaultExt = ".pdf", Filter = "PDF documents (.pdf)|*.pdf" };
        dlg.ShowDialog();
        if (!string.IsNullOrEmpty(dlg.FileName))
        {
            try
            {
                string destino = red + listPdf[CurrentPdf];
                if (File.Exists(destino))
                {
                    File.Delete(destino);
                    File.Copy(dlg.FileName, destino);
                }
                else File.Copy(dlg.FileName, destino);
                pdfViewer.LoadFile(destino);
            }
            catch (Exception ex){MessageBox.Show("Error \n" + ex.Message);}
        }

upload code

pdfViewer.LoadFile(red + listPdf[CurrentPdf = 0]);
    
asked by Luis Alberto Acosta 30.01.2018 в 18:22
source

1 answer

1

I guess the problem comes because the user blocks the file on the network (I guess it's a remote file because of your code)

I propose the following solution: Connect the specific folder as a network unit of your team and force the deletion of the file by console.

strCmdText= "del /f <ruta/del/archivo.pdf";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

I hope this solution works for you or at least you find another way to solve it.

    
answered by 30.01.2018 / 18:44
source