Recently being making a little utility app for transporting and playing local html5/jquery based presentation from pc to ipad etc, below are some snippets of code for starting stopping other apps such as chrome browser, cmd.exe, I did these in xamarin/mono really easily however the transfer to unity was a little taxing on the more involved processes, certainly adding /C before a command line argument was new to me, strangely. Below also includes some code for zipping files in unity3d using 7-zip of 7za.exe, although I’ve used internal .net libraries before sometime 7za for a unity windows stand alone solution is certainly quick and easy.
Unity3d/.Net c# code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
//-------------- open something in notepad from application directory Process.Start("notepad.exe", System.Environment.CurrentDirectory+"/edit.xml"); //-------------- open a .pdf with default native software Process.Start(System.Environment.CurrentDirectory+"/instructions/instructions.pdf"); //-------------- close any instances of chrome and open a local html file in //kiosk mode with local file access to read data:- Process[] running = Process.GetProcesses(); foreach (Process process in running) { try { if (!process.HasExited && process.ProcessName == "chrome") { if (process.CloseMainWindow()) { process.Close(); process.WaitForExit(2000); } } } catch (System.InvalidOperationException) { } } try { //open chrome in local file access mode Process.Start("chrome.exe", "--kiosk --allow-file-access-from-files"+System.Environment.CurrentDirectory+"/index.html"); } catch { currentgui=guis.nochrome; } //-------------- zip a file using a 7zip exe (you could use the zip.net library also, //although the next step is more interesting), this works better in a thread:- string myfilename="myzip"; string directorytozip=System.Environment.CurrentDirectory+"/myfoldertozip/"; ProcessStartInfo p = new ProcessStartInfo(); p.FileName = System.Environment.CurrentDirectory+"/utils/7za.exe"; p.Arguments = "a "+System.Environment.CurrentDirectory+"/OutputFolder/"+myfilename+".zip "+directorytozip+"*"; p.WindowStyle = ProcessWindowStyle.Hidden; // Start process and wait for it to exit Process zipproc = Process.Start(p); zipproc.WaitForExit(); //-------------- zip a file using a 7zip exe this methods allows spaces in directory name!! // this presumes your app directory has three subfolders/subdirectories:- // -"outputzips","directorytozip" & "7ziplib" - that holds 7za.exe string presdirwinfix = System.Environment.CurrentDirectory; string strCmdText="a \""+presdirwinfix+"\\outputzips\\"+zipname+".zip\" \""+presdirwinfix+"\\directorytozip\\*\""; UnityEngine.Debug.Log(strCmdText); System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; startInfo.FileName = "\""+presdirwinfix+"\\7ziplib\\7za.exe\""; startInfo.Arguments = strCmdText; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); //-------------- Execute a command from the windows commandline, //in this case make the zip file an installer.exe with 7zip:- //ps:- note the /C before the argument to be passed string dir = System.Environment.CurrentDirectory; string strCmdText="/C copy /b "+dir+"\\utils\\7zSD.sfx + "+ dir+"\\utils\\config.txt + "+ dir+"\\OutputFolder\\"+myfilename+".7z "+ dir+"\\OutputFolder\\myinstallerapp.exe"; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = strCmdText; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); |