There is a button on the window form. By pressing a button, a certain text file is opened in the Notepad program. After the text file is opened, the button on the window form has the property IsEnabled = false . Close the Notepad program, the button has the property IsEnabled = true .

Is it possible to make such a button in WPF-applications? And is it possible, before launching a WPF application, to do a button availability check on opening a certain text file in Notepad?

UPDATE:

I updated my post based on the answer to my question. There are two buttons in the application window: 1) Opens the desired text document, Visibility = Visibility.Visible 2) Closes the desired text document, Visibility = Visibility.Hidden . Actually, there is no difference what property to use in the control, I chose the Visibility property as an example.

I open a text file through a WPF application, the first button disappears, the second appears. I close the opened text file through the button, the second button disappears, the first appears. Question: Now in this new situation, is it possible to somehow track a specific instance of an open text file in Notepad? I just have just such buttons (or controls, there is no difference what will be), there will be several, moreover, I will programmatically add these buttons (or controls) to the ListBox.

I also forgot to mention the restriction of opening text files in Notepad through a WPF application: I can open a specific file no more than once. Of course, I understand that in this open instance of Notepad, I can open another text file, but I don’t consider this situation at all, so suppose the user doesn’t do that (although I don’t really know how track software).

    1 answer 1

    If you yourself are going to open text files with notepad, then you can create the notepad process yourself and follow the process. A simple example:

     public class MyWnd : Window { public MyWnd() { var btn = new Button() { Content = "Open Notepad" }; btn.Click += (sender, args) => { var bt = sender as Button; bt.IsEnabled = false; var proc = new System.Diagnostics.Process(); proc.StartInfo = new ProcessStartInfo("notepad", @"D:\temp\test.txt"); proc.EnableRaisingEvents = true; proc.Exited += (s, e) => { Dispatcher.Invoke(() => { bt.IsEnabled = true; }); }; proc.Start(); }; this.Content = btn; } } 

    res

    However, you cannot determine which file is open in Notepad. That is, if you open the file in Notepad, but the user then opens another file in this Notepad - you will not know about it. The maximum that you can - is to try to monitor the availability of the file for writing, but this will not give you guarantees that the file is closed in a notebook or opened in a notebook.

    UPD

    If you follow the created process and keep a link to it, you can manually kill (kill) the process or, for example, ask the notepad to close (CloseMainWindow), or just try to end the process (Close).

     public class MyWnd : Window { private Process _openedFile; public MyWnd() { var btnOpen = new Button() { Content = "Open Notepad" }; var btnClose = new Button() {Content = "close"}; btnOpen.Click += (sender, args) => { var bt = sender as Button; bt.IsEnabled = false; var proc = new System.Diagnostics.Process(); proc.StartInfo = new ProcessStartInfo("notepad", @"D:\temp\test.txt"); proc.EnableRaisingEvents = true; proc.Exited += (s, e) => { Dispatcher.Invoke(() => { bt.IsEnabled = true; }); }; _openedFile = proc; proc.Start(); }; btnClose.Click += (sender, args) => { if (_openedFile?.HasExited == false) { // Аккурантей выбирайте, что вам тут надо сделать //_openedFile.Kill(); //_openedFile.Close(); _openedFile.CloseMainWindow(); } }; var panel = new StackPanel() {Orientation = Orientation.Vertical}; panel.Children.Add(btnOpen); panel.Children.Add(btnClose); this.Content = panel; } } 

    enter image description here

    • That is, it turns out that if I add a second button to a WPF application that will close a text file opened in Notepad, a specific instance of the Notepad program cannot close? If yes, then this is very bad, because on the instructions I need, so that I can close a specific copy of the program Notepad. And yes, I forgot to write in the question about the restriction: a text file is opened only once, that is, more than once I can not open a text file through a WPF application. - Cuurjol 5:48 pm
    • I have added my question above, if it is not difficult for you, please reread it again. Thank you for the answer to my question that you provided to me above. Unfortunately, from your answer I do not yet understand how to solve another situation described in the section UPDATE. - Cuurjol
    • @Cuurjol updated the answer - tym32167