static void dowork(string arg) { FileInfo fi = new FileInfo(arg); if (fi.Extension.ToLower() != ".exe") { throw new Exception("Not an Executable file"); } FileInfo ficonfig = new FileInfo(fi.FullName + ".config"); if (ficonfig.Exists) { XmlDocument doc = new XmlDocument(); doc.Load(ficonfig.FullName); XmlNode root = doc.DocumentElement; XmlNode myNode = root.SelectSingleNode("startup"); bool needwrite = false; if (myNode == null) { XmlElement elem = root.OwnerDocument.CreateElement("startup"); elem.InnerXml = "<supportedRuntime version=\"v4.0\" /><supportedRuntime version=\"v2.0.50727\" />"; root.AppendChild(elem); myNode = elem; needwrite = true; } XmlNode c1 = myNode.ChildNodes[0]; XmlNode c2 = myNode.ChildNodes[1]; if ((c1.Name == "supportedRuntime" && c1.Attributes["version"].Value == "v4.0") && (c2.Name == "supportedRuntime" && c2.Attributes["version"].Value == "v2.0.50727")) { if (needwrite) { doc.Save(ficonfig.FullName); } CppUtils.Info("Already exists and written."); return; } doc.Save(ficonfig.FullName); throw new Exception("Unknown content"); } else { File.WriteAllText(ficonfig.FullName, Properties.Resources.configstring); } }
// [STAThread] public static void DllMain() { if (!preRun()) { return; } try { parseCommand(new List <string>(Environment.GetCommandLineArgs()).GetRange(1, Environment.GetCommandLineArgs().Length - 1).ToArray()); } catch (OkException ex) { CppUtils.Info(ex.Message); return; } catch (Exception ex) { CppUtils.Fatal(ex.Message); return; } string livingfile = Path.Combine(ConfigDir, "running"); FileStream fsRunning = null; // string pidfile = Path.Combine(ConfigDir, "pid"); // Duplicate instance check only needed if app launch without argument // = normal launch if (String.IsNullOrEmpty(Program.ApplyInventory)) { try { fsRunning = File.Open(livingfile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); //FileStream fsPid = File.Open(pidfile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); //int pid = Process.GetCurrentProcess().Id; //byte[] b = BitConverter.GetBytes(pid); //fsPid.Write(b, 0, b.Length); //fsPid.Close(); } catch (Exception) { // multiple instance try { //FileStream fsPid = File.Open(pidfile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite); //byte[] b = new byte[4]; //Array.Clear(b, 0, b.Length); //fsPid.Read(b, 0, 4); //int pid = BitConverter.ToInt32(b, 0); //Process.GetProcessById(pid); // OK, anothor process is running Process[] p = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Application.ExecutablePath)); // Activate the first application we find with this name if (p.Length > 0) { BringWindowToFront(p[0].MainWindowHandle); ShowWindow(p[0].MainWindowHandle, ShowWindowEnum.Restore); // MessageBox.Show(p[0].MainWindowHandle.ToString()); } else { //string handlefile = Path.Combine(ConfigDir, "winhandle"); //using (StreamReader sr = new StreamReader(handlefile)) //{ // string s = sr.ReadLine(); // long l; // long.TryParse(s, out l); // SetForegroundWindow((IntPtr)l); //} } return; } catch (Exception) { // still failed, uncertain what is going on , then continue. } } } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); if (fsRunning != null) { fsRunning.Close(); } }
private void btnAbout_Click(object sender, EventArgs e) { CppUtils.Info(string.Format("{0} {1}", Application.ProductName, AmbLib.getAssemblyVersion(Assembly.GetExecutingAssembly(), 3))); }