The C# equivalent [closed]
I am trying to close the program :HHTCntrl.exe as if the user clicked exit on that program. I have the following Borland C++ code. What is the C# equivalent?
FILE *sf;
AnsiString ClosePollControl = AnsiString("HHT")+cbHHTNo->Text+AnsiString(".cls");
sf = fopen(ClosePollControl.c_str(),"w");
fclose(sf);
You're opening the file for writing. Assuming that cbHHTNo
is the string
containing file name, in C# it will go like that:
var path = "HHT" + cbHHTNo + ".cls";
using (var file = File.OpenWrite(path))
{
// do sth with the open file stream here
}
Well, it is actually pretty simple.
There isn't a concept of an AnsiString
in C# so you have to use a string
.
string ClosePollControl = "HHT" + cbHHTNo->Text + ".cls";
// Open the stream and write to it.
using (FileStream fs = File.OpenWrite(ClosePollControl))
{
}
This assumes you have some user level control, cbHHTNo
, that you are getting the Text
(e.g. string
).
If you are trying to shutdown the process then use System.Diagnostics.Process class. For example,
Process process = Process.GetProcessesByName("HHTCntrl.exe").FirstOrDefault();
if (process != null)
{
process.Kill(); // to immediatelly kill the process or use process.CloseMainWindow() to gracefully "kill" the process
}
If you want to close an active form use Close() method.
精彩评论