Domanda


Ho un'applicazione (WinForms), che scarica un file nella cartella temporanea dell'utente, allora si apre il file per l'utente di vedere i contenuti, e quando il file viene chiuso, il file viene eliminato dalla cartella Temp. L'applicazione è ok funziona se apro Diciamo che uno .pdf e .doc uno Il problema appare quando si cerca di aprire uno .doc se un altro processo winword è ancora runing (non importa se non è aperto da mia app o direttamente dall'utente) .

Sto utilizzando il seguente codice:

_OpenFileProces = System.Diagnostics.Process.Start(TempFileName);
_OpenFileProces.EnableRaisingEvents = true;
_OpenFileProces.Exited += new EventHandler(_OpenFileProces_Exited);

e questo a temperatura chiaro

void _OpenFileProces_Exited(object sender, EventArgs e)
    {
        string s = ((System.Diagnostics.Process)sender).StartInfo.FileName;
        System.IO.File.Delete(s);
    }

Sembra che il processo in esecuzione si ferma la mia .. e a causa di arresto sarà eliminare il file o si genererà un errore durante il tentativo di eliminare il file.
Avete qualche suggerimento come posso aprire il mio processo? Il fatto è che non so che tipo di file devo aprire (potrebbe essere qualsiasi cosa) e sto contando su Windows di scegliere la migliore applicazione. dal mio test, blocco note funziona bene, ma winword e Acrobat si chiude il mio processo.
Grazie

È stato utile?

Soluzione

I suspect that Microsoft Word is doing exactly the same thing here as Raymond Chen describes the Windows Shell as doing here:

A customer wanted help with monitoring the lifetime of an Explorer window.

"We want to launch a copy of Explorer to open a specific folder, then wait until the user closes the folder before continuing. We tried launching a copy of Explorer with the folder on the command line, then doing a Wait­For­Single­Object on the process handle, but the wait sometimes completes immediately without waiting. How do we wait until the user closes the Explorer window?"

This is another case of solving a problem halfway and then having trouble with the other half.

The reason that Wait­For­Single­Object returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your Wait­For­Single­Object returns immediately.

In your case, Word is already running, so when you create a second Word process and instruct it to open your document, it simply hands the request off to the instance of Word that is already running, and quits the second process you launched immediately.

That's what you're seeing when you describe that "the running process is stopping my own". Because that second instance gets closed immediately after you launch it, the Exited event is raised and your code tells it to delete the file!

You astutely observe that Notepad (unlike Word and Adobe Acrobat) works just fine. That's because Notepad is designed to be a multiple-instance application. You can open as many copies of Notepad as you want; it doesn't care if there's already 1 or 6 copies open on the desktop. And more importantly, asking the shell to open a text document in Notepad actually opens a second copy of the Notepad application, rather than sending a request to the first instance to open a new window for the new doc.

Altri suggerimenti

You should set the Process.StartInfo.UseShellExecute to true like this _OpenFileProces.StartInfo.UseShellExecute = true; before starting the process and then it should work I think...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top