This button opens a text file which has error log information:
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
Process.Start(AppVars.ErrorLogFilePath);
}
When the user goes to do any processing in the application, I want to check whether the file is open or not, if it is open, then I want to close it. How would I go about doing this?
This example is almost identical to what you're trying to do:
Process.Close Method
Process myProcess;
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
myProcess.Start(AppVars.ErrorLogFilePath);
}
private void doSomething()
{
if (!myProcess.HasExited)
{
myProcess.CloseMainWindow();
myProcess.Close();
}
// Do whatever you need with the file
}
Shows how to check if it's running and how to close it.
Related
i have a coursework about nfc read and write, but i'm still new using C# and i found some libraries about nfc c#. one of them from h4kbas which I'm currenly using righ now.
the first thing i try is how to read UID, but it seem work fine
private void button1_Click(object sender, EventArgs e)
{
if (NFC.Connect())
{
textBox1.Text = NFC.GetCardUID();
}
}
and then i try making read and write :
Read
private void button2_Click(object sender, EventArgs e)
{
if (NFC.Connect())
{
NFC.WriteBlock(textBox2.Text, "5");
//Close();
}
}
Write
private void button3_Click(object sender, EventArgs e)
{
if (NFC.Connect())
{
NFC.ReadBlock("5");
//Close();
}
}
but after i try making a read and write function above, i don't found any error massage but if i try read or write some string data on running process, forced to close and give massage "The program '[10192] WFANFCReadWriteExecute.exe' has exited with code -1 (0xffffffff)." still..., i don't know the problem is on my code or i have to use "AuthBlock" first.
So I'm trying to make a simple command prompt using ConsoleControl latest version. My code is very simple :
public CommandPrompt()
{
InitializeComponent();
}
private void CommandPrompt_Load(object sender, EventArgs e)
{
consoleControl1.StartProcess("cmd", string.Empty);
}
private void CommandPrompt_FormClosing(object sender, FormClosingEventArgs e)
{
consoleControl1.StopProcess();
}
The problem is on FormClosing event. Whenever I close the window and re-open it just after (sometimes it triggers the exception just when closing), it produces a "Cannot access a disposed object" exception.
I can't seem to fix it. The problem is the StopProcess() method as I saw myself. So I've decided to manually terminate the cmd process using Process.Kill(), but still the same exception. It seems like, for some reason, terminating the cmd process when the form is closing throws this exception.
The only way that I found to terminate the process via the program is by using the command taskkill or tskill. But I want it to automatically terminate after the form closes.
Any help is greatly appreciated!
I'm answering my own question because I found the solution.
I just had to use an instance of the form and hide it instead of closing it.
Here's my code now :
public CommandPrompt()
{
InitializeComponent();
}
private void CommandPrompt_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
private void CommandPrompt_Shown(object sender, EventArgs e)
{
consoleControl1.StopProcess();
consoleControl1.StartProcess("cmd", string.Empty);
}
And when showing the form :
private CommandPrompt cmd = new CommandPrompt();
private void Button19_Click(object sender, EventArgs e)
{
cmd.Show();
}
Hope it can help other people too!
I Get this error Error 1 An object reference is required for the non-static field, method, or property 'System.Diagnostics.Process.CloseMainWindow()'
I get that error on Process.CloseMainWindow();
Could you tell me where my mistake is? I'm trying to make it that when you click x on the c# win form app it to kill the process.
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Process.CloseMainWindow();
}
The Process class is used to handle external processes.
You need to get the Process you want to close.
Process process = Process.GetProcessesByName("myProcess").FirstOrDefault();
if (process != null)
{
process.CloseMainWindow();
}
But if you are closing a child form and you want it to exit your application use
Environment.Exit(0);// or whatever exit code you need
or as #Nico Schertler metioned
Application.Exit();
But if this is your main form and you click X is should close without any other code.
I used Application.Exit(); - just as Nico Schertler said. (Thanks btw)
Here's the working code:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
You must change your method to static
private static void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Process.CloseMainWindow();
}
i make an Excel COM Add-In.
Now i want to Control the WorkbookBeforeClose Event.
But the Solution that i found on Stackoverflow (c# using the event handler WorkbookOpen) works not for me.
In my Add-In there is no InternalStartup Sektion, there is only a Load Sektion.
I can only Start the Eventhandler by pushing a Button in my Ribbon Bar.
When i push the Button 1x the Eventhandler do what i want all the Time Excel is loaded.
When Excel is closed and restart i must Push the Button again.
Is there a Way that the Eventhandler ist Starting while Excel loads the first empty Workbook ???
Is there some Help or any Solution for me ???
Thanks and Regards
Stephan
Here is my Code, that i use for the EventHandler:
public partial class Excel2SE
{
Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
private void Excel2SE_Load(object sender, RibbonUIEventArgs e)
{
EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
}
private void btn_Start_Click(object sender, RibbonControlEventArgs e)
{
wbEventhandlerStart();
}
private void wbEventhandlerStart()
{
Excel.Application app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
app.WorkbookBeforeClose += EventDel_BeforeBookClose;
}
// This Works when Excel Close
private void Excel2SE_Close(object sender, EventArgs e)
{
MessageBox.Show("Excel close");
}
// This works after Pushing the Start Button and the Workbook Close
private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
{
MessageBox.Show("WB close");
}
Is this a Visual Studio Office Add-In? It sounds like it is, if so check ThisAddIn.cs for the following 2 methods:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
}
You can add the code you want to run on close to the ThisAddIn_Shutdown method. There's no way to ensure this code runs however, it will only run if the user closes Excel 'properly'. If they close using the task manager etc, all bets are off.
I have created code in WPF to let the window remember its last location like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
Rect storedLoc = Properties.Settings.Default.WindowSavedLocation;
this.Top = storedLoc.Top;
this.Left = storedLoc.Left;
}
catch
{
MessageBox.Show("No settings stored !");
}
}
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WindowSavedLocation = RestoreBounds;
Properties.Settings.Default.Save();
}
When I build the application I can see that the app.exe.config file has the setting
WindowSavedLocation
but it just does not save and throws no exception.
Everytime I run the application, it says "No settings stored !".
It's scope is user.
I repro. The Remarks section of the Window.RestoreBounds property docs is relevant to your problem:
If you query RestoreBounds before the
window has been shown or after it has
been closed, Empty is returned.
Use the Closing event instead so the RestoreBounds property is still valid.