How to temporarily block a file?

1

I'm trying like this:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string filePath = openFileDialog.FileName;
Process processo = Process.Start(filePath);
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
processo.WaitForExit();
MessageBox.Show("Wow!");
fileStream.Close();

It seems that fileStream blocks the file before it is opened, so from the error, one of the errors it gives is this:

---------------------------
Desenhar matriz.exe - This application could not be started.
---------------------------
This application could not be started.

Do you want to view information about this issue?
---------------------------
Sim   Não   
---------------------------
    
asked by anonymous 29.11.2016 / 02:03

1 answer

0

Try this (I've added a check for it if it's already open)

        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.ShowDialog();
        string filePath = openFileDialog.FileName;

        if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(filePath)).Count() > 0)
            MessageBox.Show("O processo já está em execução!");
        else
            Process.Start(filePath);

        using (File.Open(filePath, FileMode.Open, System.IO.FileAccess.Read))
        {
            MessageBox.Show("Wow!");
        }
    
29.11.2016 / 12:20