. Net remoting Method for simple online upgrade (next article: restart exe),. netremoting
I. Preface
The previous article used the. Net Remoting technology to solve the local and server version comparison and download the update package process.
This article mainly applies Process to restart the program.
ScenarioHypothesis:
Revit2016 is loading a dll whose version is 1.0.0.0. The server's latest dll version is 1.0.0.10.
After downloading, disable Revit2016, delete the old dll, copy the new dll to the path of the old dll, and restart Revit2016.
Ii. Code
Insert the following code before line 79-80 of the last code in the previous article:
Bgk_Update.ReportProgress (100, "Updating"); Thread t = new Thread (new ThreadStart (Update); t. Start ();
Execute the Update function:
Public void Update () {string exepath = string. empty; Process [] ps = Process. getProcessesByName ("Revit"); foreach (Process p in ps) {exepath = p. mainModule. fileName. toString (); // obtain the exe path of the process p. kill (); // close this process} foreach (var module in serverupdatefiles. keys) // Delete the original module {System. threading. thread. sleep (1*1000); // make a delay, because the process response speed is slow, it may cause an error such as: Access denied... file or the application path System is not found. IO. file. delete (Path. combine (Config. dir, module + ". dll "); // copy the new file to the folder of the old file} CopyDir (Config. tempDir, Config. dir); System. diagnostics. process. start (exepath); // restart the exe}
Copy the content of a folder to another folder:
Public static void CopyDir (string srcPath, string aimPath) {try {// check whether the target directory ends with a directory delimiter. if not, add the if (aimPath [aimPath. length-1]! = Path. DirectorySeparatorChar) aimPath + = Path. DirectorySeparatorChar; // if (! Directory. exists (aimPath) Directory. createDirectory (aimPath); // obtain the file list of the source directory, this is an array containing files and Directory paths. // if you point to a file under the copy target file and do not include a Directory, use the following method string [] fileList = Directory. getFiles (srcPath); // string [] fileList = Directory. getFileSystemEntries (srcPath); // traverses all files and directories. foreach (string file in fileList) {// first, it is processed as a Directory. if this Directory exists, recursively Copy the files under this Directory if (Directory. exists (file) CopyDir (file, aimPath + Path. getFileName (fi Le); // otherwise, Copy the else File directly. copy (file, aimPath + Path. getFileName (file), true) ;}} catch {Console. writeLine ("cannot be copied! ");}}