File: Program.cs

package info (click to toggle)
keepass2 2.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,496 kB
  • sloc: cs: 87,098; xml: 6,569; cpp: 311; makefile: 49; sh: 9
file content (114 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2009 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;

using Microsoft.Win32;

namespace ShInstUtil
{
	public static class Program
	{
		[STAThread]
		public static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			if((args == null) || (args.Length == 0)) return;

			string strCmd = args[0];
			if(string.IsNullOrEmpty(strCmd)) return;
			strCmd = strCmd.ToLower();
			strCmd = strCmd.Trim(new char[]{ '\r', '\n', ' ', '\t', '-', '/',
				'\"', '\'' });

			if(strCmd == "ngen_install")
			{
				UpdateNativeImage(false);
				UpdateNativeImage(true);
			}
			else if(strCmd == "ngen_uninstall")
				UpdateNativeImage(false);
		}

		private static string FindNGenPath()
		{
			RegistryKey kSoftware = Registry.LocalMachine.OpenSubKey("SOFTWARE");
			RegistryKey kMicrosoft = kSoftware.OpenSubKey("Microsoft");
			RegistryKey kNet = kMicrosoft.OpenSubKey(".NETFramework");

			string strPath = (kNet.GetValue("InstallRoot") as string);

			kNet.Close();
			kMicrosoft.Close();
			kSoftware.Close();

			if(string.IsNullOrEmpty(strPath)) return null;

			DirectoryInfo di = new DirectoryInfo(strPath);
			FileInfo[] vNGens = di.GetFiles("ngen.exe", SearchOption.AllDirectories);
			if((vNGens == null) || (vNGens.Length == 0)) return null;

			strPath = null;
			DateTime dt = DateTime.MinValue.AddYears(1);
			foreach(FileInfo fi in vNGens)
			{
				if(fi.CreationTime >= dt)
				{
					strPath = fi.FullName;
					dt = fi.CreationTime;
				}
			}

			return strPath;
		}

		private static void UpdateNativeImage(bool bInstall)
		{
			try
			{
				string strNGen = FindNGenPath();
				if(string.IsNullOrEmpty(strNGen)) return;

				string strMe = Assembly.GetExecutingAssembly().Location;
				string strBasePath = Path.GetDirectoryName(strMe).Trim(new char[]{
					'\r', '\n', ' ', '\t', '\"', '\'' });
				string strToReg = strBasePath + Path.DirectorySeparatorChar + "KeePass.exe";

				string strPreCmd = (bInstall ? string.Empty : "un");

				ProcessStartInfo psi = new ProcessStartInfo();
				psi.Arguments = strPreCmd + "install \"" + strToReg + "\"";
				psi.CreateNoWindow = true;
				psi.FileName = strNGen;
				psi.WindowStyle = ProcessWindowStyle.Hidden;

				Process p = Process.Start(psi);
				p.WaitForExit(16000);
			}
			catch(Exception) { }
		}
	}
}