I have the following script succesfully openning the command prompt when my windows forms app launches
public Form1()
{
InitializeComponent();
//open command prompt
ProcessStartInfo Proc = new ProcessStartInfo();
Proc.FileName = "\"C:\\Users\\33631\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\System Tools\\Command Prompt.lnk\"";
Proc.WindowStyle = ProcessWindowStyle.Normal; //will become hidden eventually
Process.Start(Proc);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//here want to write a command in the previously opened command prompt
}
}
}
I would like to run a command in the terminal/command prompt that opens at the start of the app when I click the button.
For some context:
This will be used to open a url, eventually I will use ssh connection to open that url on another machine, hence the use of the command prompt.
Related
I'm trying to start a process upon a button press and then stop it when another button is pressed in WPF. When I click the "Start" button, the process starts normally, but when I click the "Stop" button, the process keeps running (I don't get any errors). My code is below:
private Process _process;
private void StartProcess(object sender, RoutedEventArgs e) {
string command = "/C .\\Miners\CryptoMiner1\Miner.exe";
_process = Process.Start("cmd.exe", command);
}
private void StopProcess(object sender, RoutedEventArgs e) {
_process.Kill();
}
I found the solution. Instead of creating a cmd.exe process and then running the .exe from there, I directly ran the exe like so:
Before
string process = "/C Process.exe -args"
Process.Start("cmd.exe", process);
After
Process.Start("Process.exe", "-args");
I'm in process of learning C# and WPF specifically. I have a program currently written but it's having an issue closing. When a user closes the main window, the program ends, but the process continues running in the background. Each time a new window is open, a new process is created and never ends. I attempted to fix this by adding the following code to my MainWindow.xaml.cs file:
private void InventoryUpdater_Closed(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
This closes the process but causes a window to pop up that acts like the program shutdown incorrectly and prompting the user to "Close the program". That window states, "[Application Name] has stopped working.
How can I end my application process without it prompting the user each time they want to close? Is it possible I've not properly disposed something?
So I figured out what was causing the prompt and what was causing the unending process.
The prompt had to do with my opening and closing of the other 3 windows. Here is what I changed to stop getting the error from Application.Current.Shutdown(). I changed this code:
private void settingsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
updateSettings();
e.Cancel = true;
main.Show();
Hide();
}
private void settingsButton_MouseUp(object sender, RoutedEventArgs e)
{
settingsWindow settingsWindow = new settingsWindow(this);
if (!settingsWindow.settingsOpen)
{
settingsWindow.Show();
Hide();
}
}
To this code:
private void settingsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
updateSettings();
}
private void settingsButton_MouseUp(object sender, RoutedEventArgs e)
{
settingsWindow settingsWindow = new settingsWindow(this);
if (!settingsWindow.settingsOpen)
{
settingsWindow.ShowDialog();
}
}
Once I close main it was going back through the other window instances that I had initialized on load and was cycling through their Closing events. Once it was getting to main.Show(), it was failing to Show something that had already been closed.
Once I did that, I was about to figure out that the original initializing of the additional windows in my Loaded event was causing the additional trace backs that weren't ending. Here is what I changed. I changed this:
private void InventoryUpdater_Loaded(object sender, RoutedEventArgs e)
{
settingsWindow settings = new settingsWindow(this);
sftpSettings severSettings = new sftpSettings(settings);
TimeSpan na = queueCustomTask();
Stop = true;
runManually.IsEnabled = true;
ssdRunManually.IsEnabled = true;
startAutoRun.IsEnabled = true;
}
To this:
private void InventoryUpdater_Loaded(object sender, RoutedEventArgs e)
{
TimeSpan na = queueCustomTask();
Stop = true;
runManually.IsEnabled = true;
ssdRunManually.IsEnabled = true;
startAutoRun.IsEnabled = true;
}
I'm not even sure why the initializing was still there. Everything had been switched to static global variables so removing those two lines affected nothing. Thanks for all the shoves in the right direction!
I am using the below code in visual studio to make an auto login software . The software is working fine, But the page that is displayed after login has a popup script. This forces the opening of popup url in internet explorer. I want to block the internet explorer getting opened. Can i solve this ?
private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("https://mwcp-ekm-04.adlkerala.com:8001/");
webBrowser1.Document.GetElementById("name").InnerText = "name";
webBrowser1.Document.GetElementById("id").InnerText = "66491";
webBrowser1.Document.GetElementById("accept").InvokeMember("click");
}
You can prevent the opening of a new window (popup) by subscribing to the NewWindow event and then cancel the event itself:
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
i'm having trouble finding enough resources to create the windows form application that i want.As title says i want to create an application on which i can set a timer to shutdown computer.Any example or help would be appreciated.Regards
Edit:so i've mananged to put current date/time button and individual shutdown button the problem is i dont know how to set a timer on shutdown during the program is running.
Heres what i've put so far:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonTimerStart_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
labelDate.Text = DateTime.Now.ToString();
}
private void buttonTimerStop_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void buttonShutdown_Click(object sender, EventArgs e)
{
Process.Start("Shutdown", "-s -t 0");
}
Basically one way to do it is to execute the windows shutdown executable and pass to it whatever timer parameters you want from your C# program. To execute any external program including this one from within your C# program you'll have to create a process, assign an executable to that process and start it something like this:
string path = #" C:\Windows\System32";
string shut_args = " ";
process1.StartInfo.FileName = " shutdown.exe";
process1.Start();
Here is a very detailed tutorial that shows exactly what you want to do: http://www.codeproject.com/Articles/22718/Shutdown-Remote-Using-Shutdown-exe
I think this can help :
How to shut down the computer from C#
Process.Start("shutdown","/s /t 0");
For the timing use the Windows form 'Timer'
I want to open folder from web page by clicking on a button ...
In my code when I am use a hyper link its working properly.
but when I am using a button its not working.
here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("file://LAP6//C$");
}
Can you provide me some C# code for this functionality.
Thanks
If you simply want to open some folder then you can try this way:
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo();
processInfo.FileName = "explorer.exe";
processInfo.Arguments = Server.MapPath("YourFolderName");
Response.Write(processInfo.Arguments.ToString());
System.Diagnostics.Process.Start(processInfo);
}
Reply me if it works for you.