|
Hello,
I want to execute a command line command from my C#. I have written code that works but am not as expected. The code is taken from "How to Execute a Command in C# ?" found on this site. and is :
<code>
public static int ExecuteCommand(string Command, int Timeout)
{
int ExitCode = -1;
ProcessStartInfo ProcessInfo;
Process Process;
try
{
ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
ProcessInfo.UseShellExecute = false;
ProcessInfo.RedirectStandardOutput = true;
//ProcessInfo.CreateNoWindow = false;
//ProcessInfo.UseShellExecute = false;
Process = Process.Start(ProcessInfo);
// Get the results
string result = Process.StandardOutput.ReadToEnd();
Console.WriteLine("Process Result = " + result);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Console.WriteLine("%%%%%%%%%%% EXIT CODE = " + ExitCode);
Process.Close();
}
catch (Exception e) {
Console.WriteLine("Error Processing ExecuteCommand : " + e.Message);
}
finally {
//Process = null;
//ProcessInfo = null;
}
return ExitCode;
}
</code>
The code runs, but untill I don't press "Ctrl+C" on dos prompt I don't see any results. I also want to check a line form the console text. For eg: If I find "Connected" from the output I want to close the process and return. Then when the user asks for disconnecting want to execute another command to disconnect the running application.
Why does the above code doesn't retun untill I press "Ctrl+c" on the command window (I don't want to show the command window also, right now am just showing)? Whne I press Ctrl+C I see the text on Console (output window), but want to check the text and on findling "Connected" want ot return and/or close the process.
Can anyone point out where am I going wrong and how to solve the problem and work out with it.
Any help is highly appreciated. Need to solve this early have already spend 2-3 days trying to solve the problem.
Thanks
Thanks & Regards,
|
|
|
|
|
|
Yes Sorry. I found this after posting over there and didn't see the same post over here. So was wondering both are different and will that post be replied or not.
So had posted here also. But now want to close this, as am geting resposne from theri also.
Thanks & Regards,
|
|
|
|
|
Hi,
I want to restrict users log on the computer from Windows "Guest" account from starting our application. Our application can be used on XP, Vista and Windows 7.
What is wrong with this solution, I get SecurityException when testing from Win7 guest account, I have tried both PrincipalPermission(null, "Guests"); and PrincipalPermission(null, "Guest");
I have also tried to use PrincipalPermission(null, "Administrators"); and then I can use the application from Admin account but not from guest account.
static void Main()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
PrincipalPermission principalGuest = new PrincipalPermission(null, "Guest"); //Has also tested with "Guests"
try
{
principalGuest.Demand();
}
catch (SecurityException e)
{
//I only want to get this exception if the user is Windows account type "guest"
}
}
Best regards
Olof
|
|
|
|
|
have you tried?
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(wi);
if (wp.IsInRole(WindowsBuiltInRole.Guest)) return;
Might need a bit more code but that should help
Architecture is extensible, code is minimal.
|
|
|
|
|
Hi, thanks for your answer. With this solution I can block users that belong to the Windows Guest group. I want to block user with name guest. The guest account you can turn on/off on windows.
I got an answer on another forum
string username = Environment.UserName;
if (username.ToLower() == "guest")
Application.Exit();
One problem is that the application can be used on many different language OS, if we are using chinese OS do we have to translate "guest" to chinese before or is there another way to determine this?
We can translate "guest" but it is easy if there is another way to solve it.
|
|
|
|
|
You'll need to create globalization resource files. There are a number of articles [here] at CodeProject
BTW: You'll be doing the support desk a favour if you add a MessageBox notifying "Guest" that they have insufficient permissions to use the application before exiting.
Architecture is extensible, code is minimal.
|
|
|
|
|
I've got a question already running at StackOverflow, but no-one has stepped in...
http://stackoverflow.com/questions/4850097/injecting-into-a-running-net-2-0-appdomain[^]
Thanks!
(I'll mark as answered when I get an answer there)
Don't forget to rate my post if it helped!
"He has no enemies, but is intensely disliked by his friends."
"His mother should have thrown him away, and kept the stork."
"There's nothing wrong with you that reincarnation won't cure."
"He loves nature, in spite of what it did to him."
|
|
|
|
|
dawmail333 wrote: I've got a question already running at StackOverflow, but no-one has stepped in...
Looks like you don't want to "inject" code, but read the memory of a foreign process. There's an article[^] on that
I are Troll
|
|
|
|
|
Ooh, thank you. I'll test that tonight.
Don't forget to rate my post if it helped!
"He has no enemies, but is intensely disliked by his friends."
"His mother should have thrown him away, and kept the stork."
"There's nothing wrong with you that reincarnation won't cure."
"He loves nature, in spite of what it did to him."
|
|
|
|
|
You're welcome
|
|
|
|
|
Ok, I have just about had enough...
Two systems, one laptop one desktop.
Built the same small test app on each one. Just a button that when clicked writes the following:
Debug.WriteLine("Test");
The laptop writes the 'Test' out tothe debug window just fine... the desktop doe snot.
Ready to really throw a fit here...
Any ideas before I toss things out the window?
Aaaaaaaaaaaaaaaaaaa
|
|
|
|
|
Hi Ray,
with the risk to state the obvious: the Debug class only works in a debug build. Check your "Configuration Manager". And check your project properties: in the Build pane, there may be a checkbox "define DEBUG constant" which should be checked.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Looks good to me... I even changed my setting over to release, ran it and then went back to debug and reran it thinking maybe there was a corrupted configuration or something...
No luck...
|
|
|
|
|
You could replace Debug.WriteLine by Console.WriteLine just to test the statement is reached...
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Decided to just bite the bullet and use the 'Reset all settings' option in the 'Import and Export Settings Wizard'.
Seems to have resolved the issue.
I now have my stuff back written to the Output window now...
|
|
|
|
|
|
|
Hi Guys,
I'm having trouble with an exception error after executing a query to return one record from my dataset.
If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'.
Could someone please explain what this means and where I need to look to fix it and avoid an exception?
Thanks,
Joe
|
|
|
|
|
Joe Stansfield wrote: If the query returns a result that doesn't exist within the database
What?
With so little information it is impossible to tell you where to look. Perhaps if you posted a snippet of the relevant code we may be able to help.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Have you checked the innerException of the Exception('Exception has been thrown by the target of an invocation') ??? whats it saying ???
With great code, comes great complexity, so keep it simple stupid...
|
|
|
|
|
More info is required because there are many reasons this exception can be thrown. Is it a stored procedure that you are calling? Does it always work the first time? Stuff like that
Architecture is extensible, code is minimal.
|
|
|
|
|
Provide some code.........
|
|
|
|
|
Before executing the query, try checking that the .Count of said query is positive.
This might solve your problem.
|
|
|
|
|
Sorry about the delay in getting back to you all - have been out of the office and based in Australia.
Code that executes with the query is as follows:
try
{
this.file_InfoTableAdapter.fFile_File_Open(this.salesDataSet.File_Info, ((int)(System.Convert.ChangeType(fFile_Open_File_Text_Box.Text, typeof(int)))));
this.saleInformationDataGridView.Sort(this.saleInformationDataGridView.Columns["dataGridViewTextBoxColumn9"], ListSortDirection.Descending);
photoListBox.Items.Clear();
photoListBox.Items.Add(file_NameTextBox.Text + "_a.jpg");
photoListBox.Items.Add(file_NameTextBox.Text + "_b.jpg");
photoListBox.Items.Add(file_NameTextBox.Text + "_c.jpg");
photoListBox.Items.Add(file_NameTextBox.Text + "_d.jpg");
string photoDisplay = "P:\\" + localityTextBox.Text + "\\" + file_NameTextBox.Text + "_a.jpg";
this.photoBox.ImageLocation = photoDisplay;
land_use_codeTextBox.ReadOnly = true;
land_areaTextBox.ReadOnly = true;
construction_yearTextBox.ReadOnly = true;
room_countTextBox.ReadOnly = true;
wall_construction_codeTextBox.ReadOnly = true;
roof_construction_codeTextBox.ReadOnly = true;
building_areaTextBox.ReadOnly = true;
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
I have limited knowledge about avoiding exceptions, but it is my understanding that all the code underneath the running of the query is being run even if the query returns no result? Leading to the exception?
But to be honest, I really have no idea... I do know that there is a lot I could improve in the code, but it is a very much "Learn as I go" approach... Which of course isn't the best, but I can only learn and get better!
Joe
|
|
|
|