Error while using sharpSSH in C# for multiple buttons - c#

I have 2 buttons, Connect and Send Command. In the first button I am creating the connection to my server and in the second sending a command. The codes are as below:
public void button1_Click(object sender, EventArgs e)
{
SshExec shell = new SshExec("hostname", "username", "password");
shell.Connect();
}
public void button2_Click(object sender, EventArgs e)
{
shell.RunCommand("ls");
}
Now the error is in "shell.RunCommand("ls");" I am getting "The name 'shell' does not contains in the current context". I am new to C# (mere 2 days) and do not have much idea, so please if anyone can help.
What I am expecting is no error and the command to be sent properly to the terminal when the second button is pressed.
At the end sorry for bad formatting of the code, please inform me if any additional information is required.

Right now, you are declaring the variable shell inside the button1_Click method. That means only that method can "see" the variable and use it.
In order for both of the button methods to use the variable, it must be declared outside/above at the class level.
Try this:
private SshExec shell;
public void button1_Click(object sender, EventArgs e)
{
shell = new SshExec("hostname", "username", "password");
shell.Connect();
}
public void button2_Click(object sender, EventArgs e)
{
shell.RunCommand("ls");
}

Related

Cannot Read Write NFC C# using library

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.

On button1 click create variable and on button2 click use that variable created in the button1 | C# WPF

I have the following button1 Click method:
private void button1_Click(object sender, RoutedEventArgs e)
{
string createstringvalue = MyCustomMethod();
}
When the user clicks the button1 it generates a custom string.
I want to use this custom string as an argument in a custom method used by the button2 Click method.
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(createstringvalue);
if (commandExecuted)
{
MessageBox.Show("Selected file was loaded successfully");
}
}
Currently I get an ERROR:
The name createstringvalue does not exist in the current context
I apologize if my question is quite simple. I try to learn C# , so I try different concepts.
Please mark the question as duplicate, and I will close it, if it is already answered and post in the comments the duplicate SO question.
You can make a variable outside your click event as Loocid mentioned, or you can directly read the value in button2:
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(MyCustomMethod());
if (commandExecuted) //or: if (CustomMethod2(MyCustomMethod()))
{
MessageBox.Show("Selected file was loaded successfully");
}
}
At the top of the class string createstringvalue; Define the variable as
later
private void button1_Click(object sender, RoutedEventArgs e)
{
createstringvalue = MyCustomMethod();
}

Checking if certain file\files exist

I want to make a windows form using C# to check if a file exist.
I've tried this one:
private void test_Click(object sender, EventArgs e)
{
var app1 = (#"C:\Users\frangon\Desktop\Spectrum-Check.EXE");
test.Text = File.Exists(app1).ToString();
}
If possible, I don't want to click it. I just want it to show as "True" if the file exist, or "False" if the file doesn't exist.
You can add an event to trigger when the form loads:
And then in your code behind:
private void Form1_Load(object sender, EventArgs e)
{
string app1 = #"C:\Users\frangon\Desktop\Spectrum-Check.EXE";
test.Text = File.Exists(app1).ToString();
}

How to call a method in 3 tier architecture

I have a class file cls_PurchaseValidae.cs in which i have written a code like below:
public void txtMfgDate_Validating(TextBox txtMfgDate,ErrorProvider errorProvider1,string datetoValidate)
{
//method for validating the text box.
ValidateMfgDate(txtMfgDate, errorProvider1,datetoValidate);
}
and i m calling this event in the code behind frm_Purchase.cs like this:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,datetoValidate);
}
but i m getting the error:
Error 1 The name 'datetoValidate' does
not exist in the current context
How can i resolve it.
Thanks in advance
I am not sure why do you want to pass around both TextBox txtMfgDate, and string datetoValidate? Shouldn’t datetoValidate be always txtMfgDate.Text? Anyway, you can do:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,txtMfgDate.Text);
}
if that is what you want.

c# call function from class

now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.

Categories

Resources