1 先说说重启//这部分是转载的
一、Restart方法
System.Windows.Forms.Application.Restart();
经测试发现有时候只会关闭程序,并不会重新启动
二、Process.Start()和Exit()
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
Application.Exit();
经测试发现有时候也只会关闭程序,并不会重新启动
三、进程的Start和Kill方法
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.Diagnostics.Process.GetCurrentProcess().Kill();
经测试使用进程进行重启比较稳定。
//开启新的实例
System.Diagnostics.Process.Start(Application.ExecutablePath);
//关闭当前实例
System.Diagnostics.Process.GetCurrentProcess().Kill();
Application.Exit();//退出当前项目,如果是子项目,则不会停止主项目
System.Environment.Exit(0);//停止所有项目
四:使用Process方式
Process p = new Process();
p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + “xxx.exe”;
p.StartInfo.UseShellExecute = false;
p.Start();
Application.Current.Shutdown();
未测试。
带参数重启
Process proc = new Process();
proc.StartInfo.FileName = @"MyExecutable.exe";
proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\"";
proc.Start();
————————————————
原文链接:https://blog.csdn.net/yizhou2010/article/details/129583150
我的数据库结构:
GO
/****** Object: Table [dbo].[RestartLog] Script Date: 09/04/2023 20:45:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RestartLog](
[Id] [int] IDENTITY(1,1) NOT NULL,
[RestartDate] [datetime] NULL,
[ThisPCName] [nvarchar](80) NULL,
[IPAdd] [nvarchar](80) NULL,
[CreateDate] [datetime] NULL
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重启时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RestartLog', @level2type=N'COLUMN',@level2name=N'RestartDate'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计算机名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RestartLog', @level2type=N'COLUMN',@level2name=N'ThisPCName'
GO
ALTER TABLE [dbo].[RestartLog] ADD CONSTRAINT [DF_RestartLog_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
我的DAL处理操作方法:
/// <summary>
/// 主要的数据操作-0无数据,1时间未到,2需要重启
/// </summary>
/// <returns></returns>
public ResultMsg InsertAndUpdate(int HourRestart)
{
ResultMsg msg = new ResultMsg();
try
{
string SqlStr = $@" DECLARE @IPadd nvarchar(80);
DECLARE @PCName nvarchar(100);
DECLARE @TempId int ;--0无数据,1时间未到,2需要重启
DECLARE @OutInt int;---返回数据
DECLARE @RestartHour int;---重启时间
Select @IPadd= CONVERT( nvarchar(80) ,CONNECTIONPROPERTY('CLIENT_NET_ADDRESS') ) ;
Select @PCName= HOST_NAME();
set @RestartHour={HourRestart};
if exists( SELECT * FROM [RestartLog] WHERE [IPAdd] = @IPadd )
begin
DECLARE @LastRestartTime datetime ;---上一次重启时间
Select Top 1 @LastRestartTime = [RestartDate] from RestartLog WHERE [IPAdd] = @IPadd order by CreateDate desc ;--获取当前最后一次时间
if ( datediff(hour,@LastRestartTime,getdate())>=@RestartHour)
begin
insert [RestartLog] ( [RestartDate] ,[ThisPCName] ,[IPAdd],[CreateDate]) values (GETDATE(),@PCName,@IPadd,GETDATE());--插入日志
set @OutInt = 2;--需要重启
end
else
begin
set @OutInt = 1;--时间未到不需要需要重启
end
end
else ----无数据,需要插入数据
begin
insert [RestartLog] ( [RestartDate] ,[ThisPCName] ,[IPAdd],[CreateDate]) values (GETDATE(),@PCName,@IPadd,GETDATE());
set @OutInt = 0;
end
select @OutInt; ";
msg.ReturnInt = DapperDbHelper.ExecuteScalar<int>(SqlStr);
msg.Success = true;
}
catch (Exception ex)
{
msg.Success = false;
msg.ErrMsg = ex.Message;
}
return msg;
}
我的程序:
/// <summary>
///重启操作
/// </summary>
public void RestartThisAPP()
{
ResultMsg ThisMgs = new DAL.DALRestartLog().InsertAndUpdate(12);
if (ThisMgs.Success )
{
if (ThisMgs.ReturnInt == 2)// 如果需要重启
{
if (BListBaojing != null)
{
foreach (var Oneitem in BListBaojing)
{
Oneitem.AttachOut();
Oneitem.CloseWhe();
}
}
//开启新的实例
System.Diagnostics.Process.Start(Application.ExecutablePath, "AutoRun");
//关闭当前实例
System.Diagnostics.Process.GetCurrentProcess().Kill();
Application.Exit();//退出当前项目,如果是子项目,则不会停止主项目
System.Environment.Exit(0);//停止所有项目
}
else {
}
}
}