在C#中如何实现让一个.exe文件在本机上只能运行一次. 在C#中如何实现让一个.exe文件在本机上只能运行一次. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以ini文件存储限制启动次数标志:OpenTims.ini:<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><a1:AppParams id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyProject/MyProject%2C%20Version%3D1.0.2008.26559%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"><OpenTimes id="ref-3">1</OpenTimes></a1:AppParams></SOAP-ENV:Body></SOAP-ENV:Envelope>类AppParams读、写ini文件:public class AppParams { private const string sSERIALIZED_CLASS_FILENAME = "OpenTims.ini"; private int OpenTims; private static string AppPath = System.Environment.CurrentDirectory.ToString(); public AppParams() { Initialize(); } public void Initialize() { this.OpenTims= 0; } public void Store() { // serialize the current instance FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Create); SoapFormatter sfFormatter = new SoapFormatter(); sfFormatter.Serialize(fsOutput,this); fsOutput.Close(); } public static AppParams RetrieveSerialized() { try { AppParams apToReturn; if(File.Exists(sSERIALIZED_CLASS_FILENAME)) { FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Open); SoapFormatter sfFormatter = new SoapFormatter(); apToReturn = (AppParams)sfFormatter.Deserialize(fsOutput); fsOutput.Close(); } else { apToReturn = new AppParams(); } return apToReturn; } catch(Exception ex) { throw new Exception(ex.Message); } } public int _opentims { get { return this.OpenTims;} set { this.OpenTims= value;} }主程序初始化判断:private void MyMainForm_Load(object sender, System.EventArgs e) { AppParams app; app = AppParams.RetrieveSerialized(); int opentimes = app._opentims; if( opentimes == 1 ) this.Close(); else app._opentims = 1; app.Store(); } string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;if (procName != "你的exe的名称"){ MessageBox.Show("工程名称被修改,不能启动!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); return;}if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) > 0){ MessageBox.Show("该工程已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); return;} 在.net中,有个Mutex类就是负责实现此功能的类,Mutex类是同步基元,它只向一个线程授予对共享资源的独占访问权。只要在加载的那个窗体里面把Main()方法稍微改一下就行了。static void Main() { bool bExist; Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist); if(bExist) { Application.Run(new Form1()); MyMutex.ReleaseMutex(); } else { MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } 而且用进程名字来限制的不是蛮好,也就是一个工程生成的EXE文件的名字完全被限定死了。如果别人要换个名字的话,那你又得改了 FindWindow函数互斥体System.Threading.Mutex 一般情况情况下都应该用 dugupiaoyun(独孤飘云) 提供的方法 C#如何计算当前系统运行了几年多少天 C# 开启摄像头和全局热键源码,为新手节省学习时间 查询数据的时候窗体出现假死 一个简单问题 老问题,报表的问题,帮忙看看 请问,这个TextBox事件怎么写? 请问MDI子窗体能设置成三级么?如何设置啊? 做一个add-in,编译时候出现这三个警告,怎么办啊? 刚开始学习编程,有问题请教,谢谢大家帮我解答,顶者有分。 C# tfs团队开发工具 在datagrid的显示列名称的地方加上一个checkbox? 由项目配置方式引出的更新dll无效的问题?
OpenTims.ini:<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:AppParams id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyProject/MyProject%2C%20Version%3D1.0.2008.26559%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<OpenTimes id="ref-3">1</OpenTimes>
</a1:AppParams>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
类AppParams读、写ini文件:public class AppParams
{
private const string sSERIALIZED_CLASS_FILENAME = "OpenTims.ini";
private int OpenTims;
private static string AppPath = System.Environment.CurrentDirectory.ToString(); public AppParams()
{
Initialize();
} public void Initialize()
{
this.OpenTims= 0;
} public void Store()
{
// serialize the current instance
FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Create);
SoapFormatter sfFormatter = new SoapFormatter();
sfFormatter.Serialize(fsOutput,this);
fsOutput.Close();
} public static AppParams RetrieveSerialized()
{
try
{
AppParams apToReturn;
if(File.Exists(sSERIALIZED_CLASS_FILENAME))
{
FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Open);
SoapFormatter sfFormatter = new SoapFormatter();
apToReturn = (AppParams)sfFormatter.Deserialize(fsOutput);
fsOutput.Close();
}
else
{
apToReturn = new AppParams();
}
return apToReturn;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
} public int _opentims
{
get { return this.OpenTims;}
set { this.OpenTims= value;}
}主程序初始化判断:private void MyMainForm_Load(object sender, System.EventArgs e)
{
AppParams app;
app = AppParams.RetrieveSerialized();
int opentimes = app._opentims;
if( opentimes == 1 )
this.Close();
else
app._opentims = 1;
app.Store();
}
{
MessageBox.Show("工程名称被修改,不能启动!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
return;
}if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) > 0)
{
MessageBox.Show("该工程已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
return;
}
只要在加载的那个窗体里面把Main()方法稍微改一下就行了。
static void Main()
{
bool bExist;
Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist);
if(bExist)
{
Application.Run(new Form1());
MyMutex.ReleaseMutex();
}
else
{
MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}