1. How do I write a program that runs another EXE file?
Execution of separate programs is done using the Process class. Here is a wrapper
function Exec which allows arguments to be passed to the called program, along with other
options such as whether to wait for the called program to finish, and the initial window state of
the called program:
public static void Exec(string strPath, string strArgs,
bool fWait, ProcessWindowStyle eStyle)
{
const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_ACCESS_DENIED = 5;
Process objProcess = new Process();
try
{
objProcess.StartInfo.FileName = strPath;
objProcess.StartInfo.Arguments = strArgs;
objProcess.StartInfo.WindowStyle = eStyle;
objProcess.Start();
if (fWait)
objProcess.WaitForExit();
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
MessageBox.Show(e.Message + ". Check the path.", "Error");
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
MessageBox.Show(eMessage + ". No permission to run file.", "Error");
}
}
This works by setting the values of the process object members, then calling Start to
start the process. The optional call to WaitForExit implements the wait option. The
exception block following catch is to prevent the calling program failing with an exception
if the called program cannot be found. The function is static, and if put in a static class
MyClass it can be called up MyClass.Exec.
2. How do I read in a text file?
This function shows how to loop through each line in a text file, concatenate the lines and
return them as a string value. If the text file does not exist or is empty, the function returns an
empty string without causing an exception:
public static string ReadTextFile(string strFilename)
{
string strFileText = "";
string strInput = "";
if (File.Exists(strFilename))
{
StreamReader sr = File.OpenText(strFilename);
while ((strInput == sr.ReadLine()) != null)
strFileText = strFileText + strInput + "\n";
sr.Close();
}
return strFileText;
}
The function is static, and if put in a static class MyClass it can be called up
MyClass.ReadTextFile. Note the function is not efficient with larger files since it uses
repeated string concatenation. String concatenation is not implemented efficiently by C# which
mananges its memory using garbage collection and the StringBuilder class should be used for
handling the strings. Also for larger files note that File.ReadAllText(strFilename) can be
used.
3. How do I write out a text file?
This function shows how to write a string out as a text file:
public static void WriteTextFile(string strFilename,
string strFileText)
{
if (strFilename != null && strFilename.Length > 0)
{
StreamWriter sw = File.CreateText(strFilename);
if (strFileText != null)
sw.Write(strFileText);
sw.Close();
}
}
The function is static, and if put in a static class MyClass it can be called up
MyClass.WriteTextFile. The function works by first checking the input filename - if the
filename is a null reference or the filename is an empty string, nothing can be written and no
error occurs. The code then uses a StreamWriter object (or more accurately an object of the
StreamWriter class) to create an empty text file. It then writes out the text in one chunk
and closes the file. Similarly to ReadTextFile shown elsewhere, note that
File.WriteAllText(strFilename, strFileText) can be used in place of the function shown
here.
|