在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# 动态编译 安全吗 并于sql并发时间问题 求一个标记特定文件的方法 C#中关于字符串处理的疑问 如何关掉窗体,系统没有退出。 一个有关收银问题的实现?谢谢! 全部的分数,请高手看看c#连接access数据库 高分请教oracle 数据导入ACCESS中方法 急!!求助!!懂 C++ 和 C#的进,数据类型转换 在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);
}
}