00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System.Collections.Generic;
00013 using System.IO;
00014 using System.Security;
00015 using Microsoft.Win32;
00016
00017 namespace Mcs.Epm.MicrosoftProject.mpFx
00018 {
00019 public class ProjectProfiles
00020 {
00021 private const string PROFILES_KEY = @"Software\Microsoft\Office\12.0\MS Project\Profiles";
00022
00023 public List<ProjectProfile> Profiles { get; private set; }
00024
00025 public ProjectProfiles(bool importProjectProfiles)
00026 {
00027 Profiles = new List<ProjectProfile>();
00028
00029 if (importProjectProfiles)
00030 {
00031 using (RegistryKey profilesKey = Registry.CurrentUser.OpenSubKey(PROFILES_KEY))
00032 {
00033 if (profilesKey == null)
00034 {
00035 return;
00036 }
00037
00038 string[] profiles = profilesKey.GetSubKeyNames();
00039
00040 for (int i = 0; i < profilesKey.SubKeyCount; i++)
00041 {
00042 string name = profiles[i];
00043
00044 using (RegistryKey profileKey = profilesKey.OpenSubKey(name))
00045 {
00046 if (profileKey != null)
00047 {
00048 string url = (string) profileKey.GetValue("Path");
00049 string userName = (string) profileKey.GetValue("UserName");
00050
00051 ProjectProfile profile = new ProjectProfile(name, url, userName, true);
00052
00053 Profiles.Add(profile);
00054 }
00055 }
00056 }
00057 }
00058 }
00059 }
00060
00061 public void LoadUserProfiles(string path)
00062 {
00063 if (File.Exists(path))
00064 {
00065 using (StreamReader textReader = File.OpenText(path))
00066 {
00067 string buffer;
00068
00069 while ((buffer = textReader.ReadLine()) != null)
00070 {
00071 string[] profileComponents = buffer.Split('~');
00072
00073 if (profileComponents.Length == 4)
00074 {
00075 if (FindByName(profileComponents[0]) == null)
00076 {
00077 Profiles.Add(new ProjectProfile(profileComponents[0],
00078 profileComponents[1],
00079 profileComponents[2],
00080 false));
00081 }
00082 }
00083 }
00084 }
00085 }
00086 else
00087 {
00088 File.Create(path).Dispose();
00089 }
00090
00091 Path = path;
00092 }
00093
00094 public string Path { get; private set; }
00095
00096 public class ProjectProfile
00097 {
00098 public string Name { get; set; }
00099 public string Url { get; set; }
00100 public string UserName { get; set; }
00101
00102 public string Password { get; set; }
00103
00104 public bool IsWinProjProfile { get; private set; }
00105
00106 public ProjectProfile(string name, string url, string userName, bool isWinProjProfile)
00107 {
00108 Name = name;
00109 Url = url;
00110 UserName = userName;
00111 IsWinProjProfile = isWinProjProfile;
00112 }
00113 }
00114
00115 public ProjectProfile FindByName(string name)
00116 {
00117 foreach (ProjectProfile profile in Profiles)
00118 {
00119 if (profile.Name.Equals(name, System.StringComparison.CurrentCultureIgnoreCase))
00120 {
00121 return profile;
00122 }
00123 }
00124
00125 return null;
00126 }
00127
00128 public void Save(string path)
00129 {
00130 if (File.Exists(path))
00131 {
00132 File.Delete(path);
00133 }
00134
00135 using (StreamWriter streamWriter = File.CreateText(path))
00136 {
00137 foreach (ProjectProfile profile in Profiles)
00138 {
00139 if (!profile.IsWinProjProfile)
00140 {
00141 streamWriter.WriteLine("{0}~{1}~{2}~{3}", profile.Name, profile.Url, profile.UserName, profile.IsWinProjProfile);
00142 }
00143 }
00144 streamWriter.Flush();
00145 streamWriter.Close();
00146 }
00147
00148 Path = path;
00149 }
00150
00151 public void Replace(string name, ProjectProfile newProfile)
00152 {
00153 ProjectProfile profile = FindByName(name);
00154
00155 Profiles.Remove(profile);
00156 Profiles.Add(newProfile);
00157 }
00158 }
00159 }