66 |
//path = builder.ToString(); |
//path = builder.ToString(); |
67 |
//this.ConfigFilePath = path; |
//this.ConfigFilePath = path; |
68 |
|
|
69 |
//var t = ClientConfigPaths.GetPaths(; |
ClientConfigPaths t = new ClientConfigPaths(this.AssemblyInfo, true); |
70 |
|
|
71 |
|
|
72 |
} |
} |
115 |
|
|
116 |
[FileIOPermission(SecurityAction.Assert, AllFiles = FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read)] |
[FileIOPermission(SecurityAction.Assert, AllFiles = FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read)] |
117 |
[SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] |
[SecurityPermission(SecurityAction.Assert, UnmanagedCode = true)] |
118 |
public ClientConfigPaths(string exePath, bool includeUserConfig) |
public ClientConfigPaths(Assembly assembly, bool includeUserConfig) |
119 |
{ |
{ |
120 |
|
|
121 |
_includesUserConfig = includeUserConfig; |
//_includesUserConfig = includeUserConfig; |
122 |
|
|
123 |
Assembly exeAssembly = null; |
Assembly exeAssembly = assembly; |
124 |
string applicationUri = null; |
//string applicationUri = null; |
125 |
string applicationFilename = null; |
//string applicationFilename = null; |
126 |
|
|
127 |
// get the assembly and applicationUri for the file |
//// get the assembly and applicationUri for the file |
128 |
if (exePath == null) |
//if (exePath == null) |
129 |
{ |
//{ |
130 |
// First check if a configuration file has been set for this app domain. If so, we will use that. |
// // First check if a configuration file has been set for this app domain. If so, we will use that. |
131 |
// The CLR would already have normalized this, so no further processing necessary. |
// // The CLR would already have normalized this, so no further processing necessary. |
132 |
AppDomain domain = AppDomain.CurrentDomain; |
// //AppDomain domain = AppDomain.CurrentDomain; |
133 |
AppDomainSetup setup = domain.SetupInformation; |
// //AppDomainSetup setup = domain.SetupInformation; |
134 |
_applicationConfigUri = setup.ConfigurationFile; |
// //_applicationConfigUri = setup.ConfigurationFile; |
135 |
|
|
136 |
// Now figure out the application path. |
// // Now figure out the application path. |
137 |
exeAssembly = Assembly.GetEntryAssembly(); |
// exeAssembly = Assembly.GetEntryAssembly(); |
138 |
if (exeAssembly != null) |
// if (exeAssembly != null) |
139 |
{ |
// { |
140 |
_hasEntryAssembly = true; |
// _hasEntryAssembly = true; |
141 |
applicationUri = exeAssembly.CodeBase; |
// applicationUri = exeAssembly.CodeBase; |
142 |
|
|
143 |
bool isFile = false; |
// bool isFile = false; |
144 |
|
|
145 |
// If it is a local file URI, convert it to its filename, without invoking Uri class. |
// // If it is a local file URI, convert it to its filename, without invoking Uri class. |
146 |
// example: "file:///C:/WINNT/Microsoft.NET/Framework/v2.0.x86fre/csc.exe" |
// // example: "file:///C:/WINNT/Microsoft.NET/Framework/v2.0.x86fre/csc.exe" |
147 |
if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_LOCAL)) |
// if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_LOCAL)) |
148 |
{ |
// { |
149 |
isFile = true; |
// isFile = true; |
150 |
applicationUri = applicationUri.Substring(FILE_URI_LOCAL.Length); |
// applicationUri = applicationUri.Substring(FILE_URI_LOCAL.Length); |
151 |
} |
// } |
152 |
// If it is a UNC file URI, convert it to its filename, without invoking Uri class. |
// // If it is a UNC file URI, convert it to its filename, without invoking Uri class. |
153 |
// example: "file://server/share/csc.exe" |
// // example: "file://server/share/csc.exe" |
154 |
else if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_UNC)) |
// else if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_UNC)) |
155 |
{ |
// { |
156 |
isFile = true; |
// isFile = true; |
157 |
applicationUri = applicationUri.Substring(FILE_URI.Length); |
// applicationUri = applicationUri.Substring(FILE_URI.Length); |
158 |
} |
// } |
159 |
|
|
160 |
if (isFile) |
// if (isFile) |
161 |
{ |
// { |
162 |
applicationUri = applicationUri.Replace('/', '\\'); |
// applicationUri = applicationUri.Replace('/', '\\'); |
163 |
applicationFilename = applicationUri; |
// applicationFilename = applicationUri; |
164 |
} |
// } |
165 |
else |
// else |
166 |
{ |
// { |
167 |
applicationUri = exeAssembly.EscapedCodeBase; |
// applicationUri = exeAssembly.EscapedCodeBase; |
168 |
} |
// } |
169 |
} |
// } |
170 |
else |
// else |
171 |
{ |
// { |
172 |
StringBuilder sb = new StringBuilder(MAX_PATH); |
// StringBuilder sb = new StringBuilder(MAX_PATH); |
173 |
UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), sb, sb.Capacity); |
// UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), sb, sb.Capacity); |
174 |
applicationUri = Path.GetFullPath(sb.ToString()); |
// applicationUri = Path.GetFullPath(sb.ToString()); |
175 |
applicationFilename = applicationUri; |
// applicationFilename = applicationUri; |
176 |
} |
// } |
177 |
} |
//} |
178 |
else |
////else |
179 |
{ |
////{ |
180 |
applicationUri = Path.GetFullPath(exePath); |
//// applicationUri = Path.GetFullPath(exePath); |
181 |
if (!FileUtil.FileExists(applicationUri, false)) |
//// if (!FileUtil.FileExists(applicationUri, false)) |
182 |
throw ExceptionUtil.ParameterInvalid("exePath"); |
//// throw ExceptionUtil.ParameterInvalid("exePath"); |
183 |
|
|
184 |
applicationFilename = applicationUri; |
//// applicationFilename = applicationUri; |
185 |
} |
////} |
186 |
|
|
187 |
// Fallback if we haven't set the app config file path yet. |
//// Fallback if we haven't set the app config file path yet. |
188 |
if (_applicationConfigUri == null) |
//if (_applicationConfigUri == null) |
189 |
{ |
//{ |
190 |
_applicationConfigUri = applicationUri + ConfigExtension; |
// _applicationConfigUri = applicationUri + ConfigExtension; |
191 |
} |
//} |
192 |
|
|
193 |
// Set application path |
//// Set application path |
194 |
_applicationUri = applicationUri; |
//_applicationUri = applicationUri; |
195 |
|
|
196 |
// In the case when exePath was explicitly supplied, we will not be able to |
//// In the case when exePath was explicitly supplied, we will not be able to |
197 |
// construct user.config paths, so quit here. |
//// construct user.config paths, so quit here. |
198 |
if (exePath != null) |
//if (exePath != null) |
199 |
{ |
//{ |
200 |
return; |
// return; |
201 |
} |
//} |
202 |
|
|
203 |
// Skip expensive initialization of user config file information if requested. |
//// Skip expensive initialization of user config file information if requested. |
204 |
if (!_includesUserConfig) |
//if (!_includesUserConfig) |
205 |
{ |
//{ |
206 |
return; |
// return; |
207 |
} |
//} |
208 |
|
|
209 |
bool isHttp = StringUtil.StartsWithIgnoreCase(_applicationConfigUri, HTTP_URI); |
////bool isHttp = StringUtil.StartsWithIgnoreCase(_applicationConfigUri, HTTP_URI); |
210 |
|
|
211 |
SetNamesAndVersion(applicationFilename, exeAssembly, isHttp); |
//SetNamesAndVersion(applicationFilename, exeAssembly, false); |
212 |
|
SetNamesAndVersion(exeAssembly, false); |
213 |
// Check if this is a clickonce deployed application. If so, point the user config |
|
214 |
// files to the clickonce data directory. |
//// Check if this is a clickonce deployed application. If so, point the user config |
215 |
if (this.IsClickOnceDeployed(AppDomain.CurrentDomain)) |
//// files to the clickonce data directory. |
216 |
{ |
//if (this.IsClickOnceDeployed(AppDomain.CurrentDomain)) |
217 |
string dataPath = AppDomain.CurrentDomain.GetData(ClickOnceDataDirectory) as string; |
//{ |
218 |
string versionSuffix = Validate(_productVersion, false); |
// string dataPath = AppDomain.CurrentDomain.GetData(ClickOnceDataDirectory) as string; |
219 |
|
// string versionSuffix = Validate(_productVersion, false); |
220 |
// NOTE: No roaming config for clickonce - not supported. |
|
221 |
if (Path.IsPathRooted(dataPath)) |
// // NOTE: No roaming config for clickonce - not supported. |
222 |
{ |
// if (Path.IsPathRooted(dataPath)) |
223 |
_localConfigDirectory = CombineIfValid(dataPath, versionSuffix); |
// { |
224 |
_localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); |
// _localConfigDirectory = CombineIfValid(dataPath, versionSuffix); |
225 |
} |
// _localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); |
226 |
|
// } |
227 |
} |
|
228 |
else if (!isHttp) |
//} |
229 |
{ |
//else if (!isHttp) |
230 |
|
//{ |
231 |
// If we get the config from http, we do not have a roaming or local config directory, |
// If we get the config from http, we do not have a roaming or local config directory, |
232 |
// as it cannot be edited by the app in those cases because it does not have Full Trust. |
// as it cannot be edited by the app in those cases because it does not have Full Trust. |
233 |
|
|
235 |
|
|
236 |
string part1 = Validate(_companyName, true); |
string part1 = Validate(_companyName, true); |
237 |
|
|
238 |
string validAppDomainName = Validate(AppDomain.CurrentDomain.FriendlyName, true); |
//string validAppDomainName = Validate(AppDomain.CurrentDomain.FriendlyName, true); |
239 |
|
string friendlyName = new FileInfo(exeAssembly.Location).Name; |
240 |
|
string validAppDomainName = Validate(friendlyName, true); |
241 |
string applicationUriLower = !String.IsNullOrEmpty(_applicationUri) ? _applicationUri.ToLower(CultureInfo.InvariantCulture) : null; |
string applicationUriLower = !String.IsNullOrEmpty(_applicationUri) ? _applicationUri.ToLower(CultureInfo.InvariantCulture) : null; |
242 |
string namePrefix = !String.IsNullOrEmpty(validAppDomainName) ? validAppDomainName : Validate(_productName, true); |
string namePrefix = !String.IsNullOrEmpty(validAppDomainName) ? validAppDomainName : Validate(_productName, true); |
243 |
string hashSuffix = GetTypeAndHashSuffix(AppDomain.CurrentDomain, applicationUriLower); |
//string hashSuffix = GetTypeAndHashSuffix(AppDomain.CurrentDomain, applicationUriLower); |
244 |
|
string hashSuffix = GetTypeAndHashSuffix(exeAssembly, applicationUriLower); |
245 |
|
|
246 |
string part2 = (!String.IsNullOrEmpty(namePrefix) && !String.IsNullOrEmpty(hashSuffix)) ? namePrefix + hashSuffix : null; |
string part2 = (!String.IsNullOrEmpty(namePrefix) && !String.IsNullOrEmpty(hashSuffix)) ? namePrefix + hashSuffix : null; |
247 |
|
|
262 |
_localConfigDirectory = CombineIfValid(localFolderPath, dirSuffix); |
_localConfigDirectory = CombineIfValid(localFolderPath, dirSuffix); |
263 |
_localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); |
_localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); |
264 |
} |
} |
265 |
} |
//} |
266 |
} |
} |
267 |
|
|
268 |
internal static ClientConfigPaths GetPaths(string exePath, bool includeUserConfig) |
//internal static ClientConfigPaths GetPaths(string exePath, bool includeUserConfig) |
269 |
{ |
//{ |
270 |
ClientConfigPaths result = null; |
// ClientConfigPaths result = null; |
271 |
|
|
272 |
if (exePath == null) |
// if (exePath == null) |
273 |
{ |
// { |
274 |
if (s_current == null || (includeUserConfig && !s_currentIncludesUserConfig)) |
// if (s_current == null || (includeUserConfig && !s_currentIncludesUserConfig)) |
275 |
{ |
// { |
276 |
s_current = new ClientConfigPaths(null, includeUserConfig); |
// s_current = new ClientConfigPaths(null, includeUserConfig); |
277 |
s_currentIncludesUserConfig = includeUserConfig; |
// s_currentIncludesUserConfig = includeUserConfig; |
278 |
} |
// } |
279 |
|
|
280 |
result = s_current; |
// result = s_current; |
281 |
} |
// } |
282 |
else |
// else |
283 |
{ |
// { |
284 |
result = new ClientConfigPaths(exePath, includeUserConfig); |
// result = new ClientConfigPaths(exePath, includeUserConfig); |
285 |
} |
// } |
286 |
|
|
287 |
return result; |
// return result; |
288 |
} |
//} |
289 |
|
|
290 |
internal static void RefreshCurrent() |
internal static void RefreshCurrent() |
291 |
{ |
{ |
293 |
s_current = null; |
s_current = null; |
294 |
} |
} |
295 |
|
|
296 |
internal static ClientConfigPaths Current |
//internal static ClientConfigPaths Current |
297 |
{ |
//{ |
298 |
get |
// get |
299 |
{ |
// { |
300 |
return GetPaths(null, true); |
// return GetPaths(null, true); |
301 |
} |
// } |
302 |
} |
//} |
303 |
|
|
304 |
internal bool HasEntryAssembly |
internal bool HasEntryAssembly |
305 |
{ |
{ |
441 |
// Returns a type and hash suffix based on app domain evidence. The evidence we use, in |
// Returns a type and hash suffix based on app domain evidence. The evidence we use, in |
442 |
// priority order, is Strong Name, Url and Exe Path. If one of these is found, we compute a |
// priority order, is Strong Name, Url and Exe Path. If one of these is found, we compute a |
443 |
// SHA1 hash of it and return a suffix based on that. If none is found, we return null. |
// SHA1 hash of it and return a suffix based on that. If none is found, we return null. |
444 |
private string GetTypeAndHashSuffix(AppDomain appDomain, string exePath) |
//private string GetTypeAndHashSuffix(AppDomain appDomain, string exePath) |
445 |
|
private string GetTypeAndHashSuffix(Assembly appDomain, string exePath) |
446 |
{ |
{ |
447 |
string suffix = null; |
string suffix = null; |
448 |
string typeName = null; |
string typeName = null; |
469 |
} |
} |
470 |
|
|
471 |
// Mostly borrowed from IsolatedStorage, with some modifications |
// Mostly borrowed from IsolatedStorage, with some modifications |
472 |
private static object GetEvidenceInfo(AppDomain appDomain, string exePath, out string typeName) |
//private static object GetEvidenceInfo(AppDomain appDomain, string exePath, out string typeName) |
473 |
|
private static object GetEvidenceInfo(Assembly appDomain, string exePath, out string typeName) |
474 |
{ |
{ |
475 |
ControlEvidencePermission.Assert(); |
ControlEvidencePermission.Assert(); |
476 |
Evidence evidence = appDomain.Evidence; |
Evidence evidence = appDomain.Evidence; |
563 |
return new StrongName(sn.PublicKey, sn.Name, new Version(0, 0, 0, 0)); |
return new StrongName(sn.PublicKey, sn.Name, new Version(0, 0, 0, 0)); |
564 |
} |
} |
565 |
|
|
566 |
private void SetNamesAndVersion(string applicationFilename, Assembly exeAssembly, bool isHttp) |
//private void SetNamesAndVersion(string applicationFilename, Assembly exeAssembly, bool isHttp) |
567 |
|
private void SetNamesAndVersion(Assembly exeAssembly, bool isHttp) |
568 |
{ |
{ |
569 |
Type mainType = null; |
Type mainType = null; |
570 |
|
|
621 |
} |
} |
622 |
} |
} |
623 |
|
|
624 |
if (versionInfoFileName == null) |
//if (versionInfoFileName == null) |
625 |
{ |
//{ |
626 |
versionInfoFileName = applicationFilename; |
// versionInfoFileName = applicationFilename; |
627 |
} |
//} |
628 |
|
|
629 |
if (versionInfoFileName != null) |
if (versionInfoFileName != null) |
630 |
{ |
{ |