c# 定期重启程序操作
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
GOSET QUOTED_IDENTIFIER ON
GOCREATE 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]GOEXEC 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'
GOEXEC 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'
GOALTER TABLE [dbo].[RestartLog] ADD CONSTRAINT [DF_RestartLog_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
我的DAL处理操作方法:
/// <summary>/// 主要的数据操作/// </summary>/// <returns></returns>public ResultMsg InsertAndUpdate(int HourRestart){ResultMsg msg = new ResultMsg();try{string SqlStr = $@" DECLARE @IPadd nvarchar(80); ---Ip地址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 if exists( SELECT * FROM [RestartLog] WHERE [IPAdd] = @IPadd and datediff(hour,RestartDate,getdate())>@RestartHour)begin update [RestartLog] set RestartDate = GETDATE() WHERE [IPAdd] = @IPadd and datediff(hour,RestartDate,getdate())>@RestartHour;set @OutInt = 2;--需要重启end elsebeginset @OutInt = 1;--时间未到不需要需要重启end end else ----无数据,需要插入数据begininsert [RestartLog] ( [RestartDate] ,[ThisPCName] ,[IPAdd],[CreateDate]) values (GETDATE(),@PCName,@IPadd,GETDATE());set @OutInt = 0; endselect @OutInt; ";msg.ReturnInt = DapperDbHelper.ExecuteScalar<int>(SqlStr);msg.Success = true;}catch (Exception ex){msg.Success = false;msg.ErrMsg = ex.Message; }return msg;}