c#?定期重啟程序操作的實(shí)現(xiàn)
一、Restart方法
System.Windows.Forms.Application.Restart();
經(jīng)測(cè)試發(fā)現(xiàn)有時(shí)候只會(huì)關(guān)閉程序,并不會(huì)重新啟動(dòng)
二、Process.Start()和Exit()
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); Application.Exit();
經(jīng)測(cè)試發(fā)現(xiàn)有時(shí)候也只會(huì)關(guān)閉程序,并不會(huì)重新啟動(dòng)
三、進(jìn)程的Start和Kill方法
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); System.Diagnostics.Process.GetCurrentProcess().Kill();
經(jīng)測(cè)試使用進(jìn)程進(jìn)行重啟比較穩(wěn)定。
//開(kāi)啟新的實(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)目,則不會(huì)停止主項(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();
未測(cè)試。
帶參數(shù)重啟
Process proc = new Process(); proc.StartInfo.FileName = @"MyExecutable.exe"; proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\""; proc.Start();
補(bǔ)
我的數(shù)據(jù)庫(kù)結(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'重啟時(shí)間' , @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無(wú)數(shù)據(jù),1時(shí)間未到,2需要重啟
DECLARE @OutInt int;---返回?cái)?shù)據(jù)
DECLARE @RestartHour int;---重啟時(shí)間
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;--時(shí)間未到不需要需要重啟
end
end
else ----無(wú)數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉和重啟計(jì)算機(jī)的示例代碼
- C#實(shí)現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟
- C#使用HttpHelper框架重啟路由器
- C#定時(shí)每天00點(diǎn)00分00秒自動(dòng)重啟軟件
- 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#一個(gè)定時(shí)重啟的小程序?qū)崿F(xiàn)代碼
- C#重啟遠(yuǎn)程計(jì)算機(jī)的代碼
相關(guān)文章
Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)簡(jiǎn)易五子棋源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
C#難點(diǎn)逐個(gè)擊破(2):out返回參數(shù)
之前提到ref是將原方法中的參數(shù)影響的結(jié)果返回到調(diào)用它的方法中,out與ref類似,相比之下,ref傳遞參數(shù)的地址,out是返回值。2010-02-02
使用c#實(shí)現(xiàn)微信自動(dòng)化功能
這篇文章主要介紹了使用c#實(shí)現(xiàn)微信自動(dòng)化,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
C# System.Text.Encoding使用小結(jié)
System.Text.Encoding類及其派生類提供了豐富的功能,幫助開(kāi)發(fā)者實(shí)現(xiàn)不同字符編碼之間的轉(zhuǎn)換,本文就來(lái)介紹一下C# System.Text.Encoding使用,感興趣的可以了解一下2025-07-07
使用C#實(shí)現(xiàn)將Excel轉(zhuǎn)換為Markdown表格
Markdown 表格廣泛應(yīng)用于技術(shù)文檔,README 文件以及各類靜態(tài)站點(diǎn)生成器中,本文將介紹如何使用 C# 將 Excel 文件轉(zhuǎn)換為 Markdown 表格,感興趣的小伙伴可以了解下2026-01-01

