开发者

The C# equivalent [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete开发者_Go百科, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜