I want to create an exe file using C# which will send emails. I want to invoke this exe using batch file. Should I create a Web form application or just a class library ? Can an exe be called using batch file from command prompt?
Please suggest.
Typically, if you want to call a application from a command line, you'd create a Console Application.
This allows your command prompt to call the application and (optionally) accept input from the console (prompt) as well as write output to the console (via the Console class).
You can read the command line arguments directly from the Main routine in the Console Application, as well.
Write a console app that takes arguments on the command line. You can then call this from script (i.e. a batch file). The arguments that you pass could contain all the elements of your email - the body may be a little large and I am not sure of the limit (if there is one) for the length of string args, but as long as the body is reasonable in size you will be ok.
I prefer writing "tools" such like this as console apps (over Windows apps) because you have so much flexibility in terms of executing the app. You can invoke it directly by typing in the cmd.exe (the command line), you can write a script to call it (as you have suggested), you can write another .NET application that can call it, etc... A Windows app has just one method of invocation.
Of course. Create the application, and store the binary exe in some folder (i.e., C:\folder).
If your exe has the name "name.exe", in your batch file, write
cd C:\folder
name
This works for all types of applications, and you can access command-line data in both, but it's easiest in a console application.
You would write:
cd C:\Users\asif\Desktop\EmailSender\ConsoleApplication1\bin\Debug\
::Space here
ConsoleApplication1
with the new line.
Related
I have a given C# executable and I want to execute a batch file inside this executable. The batch file sets environment variables, and some of these operations are conditional to other environment variables (IF conditions).
I want these environment variables to be set inside the executable itself, so that the environment variables are set on the current executable.
Absolutely every method that I can find on our friend google when I write something like “C# execute batch same process” seem to be based on the method System.Diagnostics.Process method, which executes the batch file on a different process.
Thank you
I have a console application. I want this console application to interpret the commands inside that batch file so that the environment variables set within that batch file are taken into account on the console application.
But you definitely don't want to write a batch script interpreter in C#.
If you want your console app to use the environment variables assigned in the batch script, then start your executable at the end of that batch script.
Your application will then inherit that environment, and therefore those environment variables.
If you can't alter said batch script, then create a new batch script that:
Runs the original batch script, then
Runs your executable.
I've an application same as command line application. I type command, it send command to server and display result on screen.
My application is an executable application on windows and has two feature that help me to easy work: record script and play script.
Now, I want to send command to running application and raise play script button to run sent command on it.
Is there any way to solve this problem in c# language?
Uh-oh. I hope I understood you correctly. I assume you have a third-party application that interacts with some server and is operated by two distinct commands, play and record. You want to write a new application that would invoke these commands somehow, and the problem is this invocation.
You can do it in C#, but the way you do it depends on the way you operate the program. I will show you some cases:
The program is a command line tool. Input is sent via command line parameters when the program is run. Output is gathered via redirections of the standard output of the program. Look at System.Diagnostics.Process.Start and its parameters, namely ProcessStartInfo which contains parameters and input/output redirection options.
The program is a console tool that is not operated via command line parameters. Use the same as above, but feed input via input redirection.
The program is a GUI program with no ability to get input in text form. This is most likely the case. Here you will have to either find a specific way to operate the program such as with an advanced technique called hooking. Or you can send commands as if a user is issuing them. Look at System.Windows.Forms.SendKeys method. It might help. How to get a result here is a big question. You can either parse the screen or monitor some state of a program or something else. No ready solution for everyone here.
I want to develop an application, App1, that gets some information & settings from the user. Then, using these settings, it should build a second application, App2. I want to have App1 build App2's exe file.
I know that one way to do this is to make a text or XML file to hold the settings and put this next to App2's exe file, but I want to embed these settings into App2's exe file instead. How can I do this using Visual Studio, the .net framework, and the C# language?
I don't understand what do you mean by those user information and settings (some example would help), but basically, what you want to do is invoke the C# compiler as follows
var p = new Process();
p.StartInfo.FileName = #"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe";
p.StartInfo.Arguments = #"c:\aa\Test.cs";
p.Start();
These four lines invoke the c# compiler on a C# code file and produces an exe file in the output folder for your project.
MSDN provides more info on how to work with the compiler via command line. Especially, how to compile more than one file, how to compile a library, etc.
You can accomplish the same thing like this:
Make two applications. App2 will be the same .exe file for all cases of App1. Instead of building an .exe file, App1 will generate a configuration file containing the information and settings input by the user. This file will be read by App2. App2 will then call the appropriate functions within App2, based on the information and settings in the configuration file.
I'm trying to do the same thing for the same reasons, but using VB.NET. That's how I found this question. The only way I can thing of (other than editing the source code each time, which is what I'll be doing unless I find a better way), is to create a second program that encrypts a parameter file and give the encrypted parameter file and the exe file to the end user.
I'm still using the "editing the source method" because I want to give the user just the exe file without dealing with parameter files or encryption. There aren't many users who will need this in my case (for the moment) so I can deal with that method for now.
How to interact .bat file with java or C#.net GUI application? Here is part of my .bat code.
:Valid
echo Enter student id:
set/p "pass=>"
if NOT %pass%== UserStudentId goto FAIL
When I run the .bat file it will ask Enter student id: in command prompt, then validate the student id and do some process.
I want to pass this input from java or C#.net IDE to .bat file and continue the process. In here User will never deal with command prompt.
If there is any output from .bat , it will show in java or .net IDE also.
I wonder if it is possible since I'm new to this. TX.
When you start a process from Java or C# you can get console input and output of that process as streams. You then can simply write into the process' input stream.
It might be, though, that the batch file won't actually like that since cmd might look for actual interactive input with set /p (e.g., it fails with file redirection, if I remember correctly). A better method, if you control the batch file itself, would be the following:
if defined pass goto SkipInput
set /p pass=Enter student ID:
:SkipInput
which will use an environment variable if present and only ask the user if it isn't already set. This is much more automation-friendly, since you can just set the environment variable in the calling process and have it inherited for every process you spawn. And no hassle with emulating input as well.
I'm a Silverlight/ASP.NET developer trying to write my first Windows Forms application to run in the background on a server, populating our database. Eventually would like this to be a Windows service, but it's not required initially.
I need to create a batch file to execute 5 instances of this application, passing in the URL to 5 RESTful endpoints. So I published my app, which created a setup.exe. After installing it, I have an item that points to
C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\, with a .appref-ms file.
I'm not sure at this point what to do. Running:
"C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\StreamingApp.appref-ms" -"http://www.myURL.com" throws up a command window briefly, but the app doesn't run, data doesn't populate in DB.
What am I missing here?
since your application is in .exe format. And make your winform accepts command line arguments (check the main method) also make your Form ctor accepts params too. Then just launch it via cmd line just as you would other command, but here only to navigate to that dir where file exists.
In case of batch, use start command followed by program name and then arguments
It's hard to understand what is happening inside your application. You need to debug to understand what is going on there when it receives given parameters.
So I would suggest to debug an EXE. For this go to your EXE project properties, select DEBUG tab in CommandLineArguments insert your parameter string.
Run it in DEBUG and hopefully you will figure out a problem.
If after debugging it's not yet clear why it behaves in that way, come back to SO :)
Silvi if you plan to use your windows forms application from a batch file and you imagine the applicationm will behave differently in such mode than when opened witha double click, the usual approach is to parse the command line (arguments, also available in the main method as parameter) and to avoid loading the UI at all.
in fact if you have written your application properly the UI only managed the UI and does not contain the whole logic of database manipulation and data transformation.
what you could do is check inside the Main method if there are command line parameters and if you detect any of the special ones you have definded you really avoid to even call Application.Run(new Form1(...)); and start working in batch mode without user interface.
the same logic you want to use in batch mode or in UI mode can be wrapped in helper classes (often also called business managers or business logic... it depends), so that you do not have code duplication but simply UI or batch will call those classes nicely.