|
|
I want to call cmd.exe from C# application and to change the directory from current directory.I have written code for this which is given below
------------------------------------------------------------------------------------------------------------------
//sample code //
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
namespace Console_sample
{
class Program
{
public void OpenWithArguments()
{
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(@"Echo on");
p.StandardInput.WriteLine(@"dir " + @"C:");
p.StandardInput.WriteLine(@"chdir " + @"C:\WINDOWS ");
p.StandardInput.WriteLine(@"EXIT");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
Console.Write(output);
p.Close();
Console.Read();
}
static void Main(string[] args)
{
Program myProcess = new Program();
myProcess.OpenWithArguments();
}
}
}
--------------------------------------------------------------------------------------
In the console I got following output
-----------------------------------------------------------------------------------------------------------
D:\Program\Console_sample\bin\Debug>Echo on
D:\Program\Console_sample\bin\Debug>dir C:
Volume in drive C has no label.
Volume Serial Number is F06C-447B
Directory of C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
04/24/2008 05:17 PM <DIR> .
04/24/2008 05:17 PM <DIR> ..
03/26/2008 04:02 PM <DIR> 1033
D:\Program\Console_sample\bin\Debug>chdir C:\WINDOWS
D:\Program\Console_sample\bin\Debug>EXIT
---------------------------------------------------------------------------------
where my directory is not chaged while the DIR command got perfectly executed .How can i fix this ?Even i notice that when i write p.standardInput.writeline(@"date"); it doean't show me the date also.please provide solutuion for this .if anyone has sample code for executing cmd command from C# .net will be very useful.
any help appreciated
thanks in advance
|
|
|
|
|
The line you've written:
<br />
p.StandardInput.WriteLine(@"chdir " + @"C:\WINDOWS ");<br />
This will take effect:
<br />
p.StandardInput.WriteLine(@"chdir /d" + @"C:\WINDOWS ");<br />
|
|
|
|
|
Hey guys...
i have an console app that calls a different console app, im trying to get the result of the 2nd app printed in the 1st app.
heres my current code
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess = Process.Start("ring.exe");
Console.WriteLine("Started 1st process --> Waiting............ ");
myProcess.WaitForExit();
StreamReader sr = myProcess.StandardOutput;
Console.WriteLine("Finnished 1st process ---> output:");
Console.WriteLine(sr.ReadToEnd());
im getting the following error at runtime "StandardOut has not been redirected or the process hasn't started yet."
any ideas what im doing wrong?
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
try this:
<br />
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");<br />
System.Diagnostics.Process p = new System.Diagnostics.Process();<br />
psi.RedirectStandardOutput = true;<br />
psi.UseShellExecute = false;<br />
p.StartInfo = psi;<br />
p.Start();<br />
p.WaitForExit();<br />
System.IO.StreamReader sr = p.StandardOutput;<br />
MessageBox.Show("Finnished 1st process ---> output:");<br />
MessageBox.Show(sr.ReadToEnd());<br />
|
|
|
|
|
i want to check through my C# code, Pen Drive Connect to computer yes or not only.
|
|
|
|
|
I don't think there's an event for that, but you CAN iterate over available drives and work out if any are removable. DriveInfo[] drives = DriveInfo.GetAllDrives() ( something like that ). Then, each one has an enum that tells you if it's a HDD, if it's optical, if it's removable.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
|
Hi,
Can someone try to tell me the definitive (like, yeah) way of sending a mail in c# from my website. I'm .NET2, Visual Sudio 2005 and have a 3 simple textboxs and a send button on my site for people to get in touch. I want it to send me the text1 as a subject, text2 as the body, text3 as their mail address (which they have manually typed in)
Obviously I've been round the web.....
private void Button1_Click(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To = txtTo.Text;
mail.From = txtFrom.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mail);
}
these seem out of date, and how do I test locally on my machine? Do I put 'localhost' as my SMTP server? until it goes live?
Any help would be great.
Thanks
Harvey
|
|
|
|
|
imnotso# wrote: MailMessage mail = new MailMessage();
mail.To = txtTo.Text;
mail.From = txtFrom.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mail);
This is obsolete now. Use classes in System.Net.Mail namespace. You can configure System.Net.Mail settings on the app.config file and put the server name as localhost . Stop local SMTP server and set mail delivery type as PickupDirectoryFromIis which will deliver mails to the pickup directory.
|
|
|
|
|
Thanks for that,
In the new classes I cannot find where I specify the SMTP server name. Any ideas?
Harvey
|
|
|
|
|
Plus 'localhost' always returns an error.
|
|
|
|
|
Then I guess you found where to put it ? You need an actual mail server, in order to send mail.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Yeah, thanks.
Why is everyone mentioning 'localhost' if it's not a vaible option i wonder? Can I just use any server until I put it live?
Thanks
|
|
|
|
|
It's possible to set up a mail server on our local machine. You can put whatever you like, but it won't work, obviously.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I'm not sure how it can be tested then without putting it live, that cannot be right surely?
|
|
|
|
|
How do you hope to send mail, without a mail server ? You cannot. I don't see the issue, your mail server is not 'live', it's the server that sends your mail ( i.e. you can use the mail server from your ISP, the one that your Outlook uses ).
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Just for testing purpose you can use localhost as server and delivery method as Pickup directory. So all the mails sent are kept in pickup directory as outlook file format. You can goto that directory and easily check the mail. Once you are ready to release the application, change delivery method to Network and specify valid SMTP server details and credentials. Read details about SMTPClient here[^]. Read and excellent FAQ on System.Net.Mail here[^]. Also this[^] explains how to configure your application configuration file.
|
|
|
|
|
Thank you, I thought it was possible. many thanks again....
|
|
|
|
|
|
I have a working version of an email client I made using smtp for C#. If you still need it, I can send you my sample code. As well, I would like to tell you that the server you are using is also important. I'm going to presume that you are going to use a free server, so your best bet would most likely be using Googlemail's smtp, since you actually forward your mail to their ports. As well, what you had before was to simple a method, if you're using an smtp server, you're going to have to authenticate each request, meaning that you'll also have to send your email's password.
|
|
|
|
|
hi all,
Nw i need to create a custom regular expression for validating the string [Select]
plz help....
regards,
syamooo...
|
|
|
|
|
"[Select]"
A regex can be anything you like, that is, there are many ways to match that string. The real question is, how closely do you need to match it, and what other strings would be OK ? Expresso is a tool you should download to help you learn regex
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
What string do you want to validate for what values?
This ^ gave me loads of answers in seconds.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hello everyone,
I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly.
Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid.
Any better approaches recommended?
thanks in advance,
George
|
|
|
|