C#:自動(dòng)更新(從網(wǎng)絡(luò)共享路徑),程序刪除,從網(wǎng)絡(luò)復(fù)制,系統(tǒng)啟動(dòng)

    /// <summary>
    /// 自動(dòng)更新(從網(wǎng)絡(luò)共享路徑),程序刪除,從網(wǎng)絡(luò)復(fù)制,系統(tǒng)啟動(dòng)
    /// </summary>
    private void UpdateFromShare()
    {
        string strXCopyFiles = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XCopyFiles.bat");
        string tempUpdatePath = "\\\\192.168.1.100\\AutoUpdate\\AutomatedOrdering"; //自動(dòng)更新路徑
        string netUser = "autoupdate"; //網(wǎng)絡(luò)用戶名
        string netPassword = "autoupdate"; //網(wǎng)絡(luò)密碼

        //生成更新BAT(NET USE 獲取網(wǎng)絡(luò)讀取權(quán)限,復(fù)制文件)
        using (StreamWriter swXcopy = new StreamWriter(strXCopyFiles,false,Encoding.Default)) 
        {
            swXcopy.WriteLine(
                string.Format(
                    @"@echo off"+Environment.NewLine
                    +"net use {0} {2} /user:{1}"+Environment.NewLine
                    + "xcopy /y/s/e/v \"" + tempUpdatePath + "\" \"" + Directory.GetCurrentDirectory() + "\\\"",
                    tempUpdatePath, 
                    netUser, 
                    netPassword
                    ));
        }
        
        //生成BAT(刪除舊版本,刪除BAT,啟動(dòng)更新后的程序)
        string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "KillApp.bat");
        using (StreamWriter bat = new StreamWriter(filename, false, Encoding.Default))
        {
            // 自刪除,自啟動(dòng)
            bat.WriteLine(string.Format(@"
                    @echo off
                    :selfkill
                    attrib -a -r -s -h {0}
                    del {0}
                    if exist {0} goto selfkill
                    call XCopyFiles.bat
                    del XCopyFiles.bat "+ Environment.NewLine
                     + "\"" + Application.ExecutablePath + "\"" + Environment.NewLine 
                     + " del %0 ", 
                     AppDomain.CurrentDomain.FriendlyName
                     ));
        }
        
        // 啟動(dòng)自刪除批處理文件
        ProcessStartInfo info = new ProcessStartInfo(filename);
        info.WindowStyle = ProcessWindowStyle.Hidden;
        Process.Start(info);
        
        // 強(qiáng)制關(guān)閉當(dāng)前進(jìn)程
        Environment.Exit(0);
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容