How to block a file temporarily?

Asked

Viewed 104 times

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 to me that fileStream blocks the file before it is opened, so of 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   
---------------------------
  • Isn’t there a process thread already running? I don’t know much about C#, but this Process.Start(filePath); does not make much sense. It is trying to run the file as if it were an executable. This must be why the application ends.

  • It opens normal, the problem is when I put this fileStream command right away, I already tried to put a messagebox to intercept, so it works, the problem is, in how many seconds should I block? Got it?

  • But you understood what I said, that Process.Start(filePath); here is not making sense. What is the reason for this, you have full knowledge of what the Process.Start does and how it works. I believe that you are using the code randomly, this line is meaningless, it’s like you want the file to become an executable.

  • I want exclusive access, the intention is to run it and block in a way "gambiarra" while the process is running.

  • Icaro I understood the intention and goal, what doesn’t make sense is your code, the way you’re using Process.Start isn’t making sense, it’s that seems to be the whole problem, you know? It must be a problem that triggers another. ;)

  • But I want to run, I want to make the program really open.

  • So filePath is not a file but another executable? Or is a executable that calls another?

  • No, I run a program x and then I open it with the stream kind of to block understood?

  • I don’t understand, this confused, you want the matrix.exe program to call itself and be "blocked" using Filestream?

  • No, a C# program calls an X program and makes it impossible to copy, etc.

  • 1

    Oh understood, edit the question and put these details. Just a hint, try to change the order to FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
Process processo = Process.Start(filePath);
processo.WaitForExit(); and see if it works

  • Same thing, it’s more complicated than it looks.

Show 7 more comments

1 answer

0

Try it this way (I added a check in case it is 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!");
        }
  • No no... I want it to be run first and blocked later, so the user can neither copy nor delete.

  • Who will run? The user himself? In my code, after choosing the file "filePath" it will run and locked soon after.

  • No, it can still be copied.

  • I followed his own example and while the "Wow" message is being displayed, it will be locked (regardless of whether it is open or not). In place of msg we could create a flag, and only after "unfreezing" the file would be released for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.