There are many times a situation has occurs (such as user want to see document file, see any webpage, run any external program, etc.) where a user wants to launch another .exe file or Application in C# programming, to resolve this problem, C# provides an efficient way through which we can do it. C# has namespace namely System. Diagnostics have a class named Process which plays an important role to launch another .exe or file.
Process class has a method ‘Start()’ through which we can launch another application in C# programming. Process class access local or remote process and enables them to the user for start or stop local system process that is we can say that process class helps to start, stopping, controlling and monitoring external application. Process component provides to access the process, which is running in the system. Using the Process component we can obtain the list of the running processes in the system and we can also start the new process in the system.
Here I am giving a console application example to open directory with the help of
Process.Start() method. Let’s see a brief demonstration.
Example:
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
// open the new process of opening directory
Process.Start(@"c:\\");
}
}
}
This example sends the C:\ directory name to the operating system, which will result in an explorer opening the folder on your system or desktop.
Now, in the same manner, you can open the text files, word file and much more.
Example: Open .txt and .docx file
To open these files write the following code.
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
// process one opening for ms-word file
Process.Start(@"C:\Users\Sachindra\Desktop\TestFile.docx");
// waiting for 1 second to create second process
Thread.Sleep(1000);
// process two opening for txt file
Process.Start(@"C:\Users\Sachindra\Desktop\TestTextFile.txt");
}
}
}
Example: Open any webpage or Launch URL’S
You can open any webpage through the Process.Start() method by entering the URL of the webpage within the Start() method as an argument
as well as you can also search any content or topic via the search engine such as Google, Yahoo, etc. Let’s see a brief example of it.
Search any content through google search engine write the following code:
using System.Diagnostics;
using System.Threading;
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
// create method and pass content within it and search it in google
SearchContentOnGoogle("Trigger in sql server mindstick");
}
public staticvoidSearchContentOnGoogle(stringcontent)
{
//enter the search engine url
Process.Start("http://google.com/search?q="+content);
}
}
}
Output:
Now launch any web page URL such as:
using System.Diagnostics;
using System.Threading;
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
// Launch any other url or web page
Process.Start("http://mindstick.com");
}
}
}
Output:
Example: Launch Legacy Program or .exe file
You can launch or run any executable program through the process component. Here I am taking an old console application and run it with the help of the ProcessStartInfo class which has many properties.
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
// Create object of ProcessStartInfo class
ProcessStartInfo stinfo= new ProcessStartInfo();
// Assign file name
stinfo.FileName= @"C:\Users\Sachindra\Desktop\Logic.exe";
// start the process without creating new window default is false
stinfo.CreateNoWindow= true;
// true to use the shell when starting the process; otherwise, the process is created directly from the executable file
stinfo.UseShellExecute= true;
// Creating Process class object to start process
Process myProcess= Process.Start(stinfo);
// start the process
myProcess.Start();
}
}
}
Output:
Example: Start-Process as Administrator Permissions
To run any process as administrator rights writes the following code.
namespace ConsoleProcessTest
{
class Program
{
static voidMain(string[] args)
{
try
{
// create process instance
Process myprocess= newProcess();
// set the file path which you want in process
myprocess.StartInfo.FileName= @"C:\Users\Sachindra\Desktop\Logic.exe";
// take the administrator permision to run process
myprocess.StartInfo.Verb="runas";
// start process
myprocess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
}
}
Irfan Ali
20-Oct-2018Dear Concern!
Thanks a lot for your Awesome post.i just want to ask one thing that how can i click on a button on given Url.
Feeling hopeful for your response
Shankar Desai
21-Jan-2017ProcessStartInfo startInfo =default(ProcessStartInfo);
John Smith
21-Jan-2017Shankar Desai
21-Jan-2017Golden Rasha
16-Aug-2016BUT 1 MORE THING, WHEN I EXECUTE MY EXTERNAL EXE PROGRAM, IT OPENS UP NORMALLY AS IF I WENT TO ITS FOLDER AND DOUBLE CLICKED IT, HOW CAN I MAKE IT SO THAT THE EXE PROGRAM OPENS UP INSIDE MY WINFORM APPLICATION?
David Zeindler
22-Apr-2014Anonymous User
10-Nov-2012There are lots of technique to hide the dll path, in one of them is Encryption and Decryption technique through which you can encode your DLL path and use it.
parag patel
09-Nov-2012{
try
{
string lgin = "";
float f = AFFile.FOpen(@"d:\\localhost.dll", "r");
if (ATFloat.IsTrue(f))
{
while (ATFloat.IsFalse(AFFile.FEof(f)))
{
lgin = lgin + AFFile.FGets(f);
}
}
else
{
AFStr.printf("ERROR: file can not be found (does not exist)");
}
AFFile.FClose(f);
if (lgin.Trim() == ":)")
{
string t2 = "KIEMA()";
how can hide
i creat dll then open in notpad [ YException Exception Show .cctor #d : \ \ l o c a l h o s t . d l l r ]
how can hide this pls help k_info108@yahoo.com