c#?定期重啟程序操作的實(shí)現(xiàn)
一、Restart方法
System.Windows.Forms.Application.Restart();
經(jīng)測試發(fā)現(xiàn)有時候只會關(guān)閉程序,并不會重新啟動
二、Process.Start()和Exit()
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); Application.Exit();
經(jīng)測試發(fā)現(xiàn)有時候也只會關(guān)閉程序,并不會重新啟動
三、進(jìn)程的Start和Kill方法
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); System.Diagnostics.Process.GetCurrentProcess().Kill();
經(jīng)測試使用進(jìn)程進(jìn)行重啟比較穩(wěn)定。
//開啟新的實(shí)例 System.Diagnostics.Process.Start(Application.ExecutablePath); //關(guān)閉當(dāng)前實(shí)例 System.Diagnostics.Process.GetCurrentProcess().Kill(); Application.Exit();//退出當(dāng)前項(xiàng)目,如果是子項(xiàng)目,則不會停止主項(xiàng)目 System.Environment.Exit(0);//停止所有項(xiàng)目
四:使用Process方式
Process p = new Process(); p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + “xxx.exe”; p.StartInfo.UseShellExecute = false; p.Start(); Application.Current.Shutdown();
未測試。
帶參數(shù)重啟
Process proc = new Process(); proc.StartInfo.FileName = @"MyExecutable.exe"; proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\""; proc.Start();
補(bǔ)
我的數(shù)據(jù)庫結(jié)構(gòu):
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'計(jì)算機(jī)名' , @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>
/// 主要的數(shù)據(jù)操作
/// </summary>
/// <returns></returns>
public ResultMsg InsertAndUpdate(int HourRestart)
{
ResultMsg msg = new ResultMsg();
try
{
string SqlStr = $@" DECLARE @IPadd nvarchar(80); ---Ip地址
DECLARE @PCName nvarchar(100); --計(jì)算機(jī)名
DECLARE @TempId int ;--0無數(shù)據(jù),1時間未到,2需要重啟
DECLARE @OutInt int;---返回數(shù)據(jù)
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
else
begin
set @OutInt = 1;--時間未到不需要需要重啟
end
end
else ----無數(shù)據(jù),需要插入數(shù)據(jù)
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;
}到此這篇關(guān)于c# 定期重啟程序操作的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)c# 定期重啟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉和重啟計(jì)算機(jī)的示例代碼
- C#實(shí)現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟
- C#使用HttpHelper框架重啟路由器
- C#定時每天00點(diǎn)00分00秒自動重啟軟件
- C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- C#實(shí)現(xiàn)控制Windows系統(tǒng)關(guān)機(jī)、重啟和注銷的方法
- C#實(shí)現(xiàn)關(guān)機(jī)重啟及注銷實(shí)例代碼
- c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷)
- c#一個定時重啟的小程序?qū)崿F(xiàn)代碼
- C#重啟遠(yuǎn)程計(jì)算機(jī)的代碼
相關(guān)文章
Unity Sockect實(shí)現(xiàn)畫面實(shí)時傳輸案例原理解析
Socket是比較常用的一種通信方式,本文通過案例給大家介紹Unity Sockect實(shí)現(xiàn)畫面實(shí)時傳輸功能,感興趣的朋友一起看看吧2021-08-08
c#調(diào)用c++的DLL的實(shí)現(xiàn)方法
本文主要介紹了c#調(diào)用c++的DLL的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04
C#結(jié)合AForge實(shí)現(xiàn)攝像頭錄像
最近由于興趣學(xué)習(xí)了下在C#上使用AForge錄制攝像頭視頻并壓縮編碼。總體上來說這個第三方.net視覺開發(fā)庫還是比較穩(wěn)定的2017-09-09

