2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Diagnostics ;
5
6
using System . Globalization ;
6
7
using System . IO ;
7
8
using System . Reflection ;
9
+ using System . Runtime . InteropServices ;
8
10
using System . Security ;
9
11
10
12
namespace System . Configuration
@@ -32,7 +34,6 @@ private ClientConfigPaths(string exePath, bool includeUserConfig)
32
34
_includesUserConfig = includeUserConfig ;
33
35
34
36
Assembly exeAssembly = null ;
35
- string applicationFilename = null ;
36
37
37
38
if ( exePath != null )
38
39
{
@@ -42,39 +43,42 @@ private ClientConfigPaths(string exePath, bool includeUserConfig)
42
43
{
43
44
throw ExceptionUtil . ParameterInvalid ( nameof ( exePath ) ) ;
44
45
}
45
-
46
- applicationFilename = ApplicationUri ;
47
46
}
48
47
else
49
48
{
50
49
// Exe path wasn't specified, get it from the entry assembly
51
50
exeAssembly = Assembly . GetEntryAssembly ( ) ;
52
51
53
- if ( exeAssembly == null )
54
- throw new PlatformNotSupportedException ( ) ;
55
-
56
- HasEntryAssembly = true ;
52
+ if ( exeAssembly != null )
53
+ {
54
+ HasEntryAssembly = true ;
57
55
58
- // The original .NET Framework code tried to get the local path without using Uri.
59
- // If we ever find a need to do this again be careful with the logic. "file:///" is
60
- // used for local paths and "file://" for UNCs. Simply removing the prefix will make
61
- // local paths relative on Unix (e.g. "file:///home" will become "home" instead of
62
- // "/home").
63
- string configBasePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , exeAssembly . ManifestModule . Name ) ;
64
- Uri uri = new Uri ( configBasePath ) ;
56
+ // The original .NET Framework code tried to get the local path without using Uri.
57
+ // If we ever find a need to do this again be careful with the logic. "file:///" is
58
+ // used for local paths and "file://" for UNCs. Simply removing the prefix will make
59
+ // local paths relative on Unix (e.g. "file:///home" will become "home" instead of
60
+ // "/home").
61
+ string configBasePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , exeAssembly . ManifestModule . Name ) ;
62
+ Uri uri = new Uri ( configBasePath ) ;
65
63
66
- if ( uri . IsFile )
67
- {
64
+ Debug . Assert ( uri . IsFile ) ;
68
65
ApplicationUri = uri . LocalPath ;
69
- applicationFilename = uri . LocalPath ;
70
66
}
71
67
else
72
68
{
73
- ApplicationUri = Uri . EscapeDataString ( configBasePath ) ;
69
+ // An EntryAssembly may not be found when running from a custom host.
70
+ // Try to find the native entry point.
71
+ using ( Process currentProcess = Process . GetCurrentProcess ( ) )
72
+ {
73
+ ApplicationUri = currentProcess . MainModule ? . FileName ;
74
+ }
74
75
}
75
76
}
76
77
77
- ApplicationConfigUri = ApplicationUri + ConfigExtension ;
78
+ if ( ! string . IsNullOrEmpty ( ApplicationUri ) )
79
+ {
80
+ ApplicationConfigUri = ApplicationUri + ConfigExtension ;
81
+ }
78
82
79
83
// In the case when exePath was explicitly supplied, we will not be able to
80
84
// construct user.config paths, so quit here.
@@ -84,7 +88,7 @@ private ClientConfigPaths(string exePath, bool includeUserConfig)
84
88
if ( ! _includesUserConfig ) return ;
85
89
86
90
bool isHttp = StringUtil . StartsWithOrdinalIgnoreCase ( ApplicationConfigUri , HttpUri ) ;
87
- SetNamesAndVersion ( applicationFilename , exeAssembly , isHttp ) ;
91
+ SetNamesAndVersion ( exeAssembly , isHttp ) ;
88
92
if ( isHttp ) return ;
89
93
90
94
// Create a directory suffix for local and roaming config of three parts:
@@ -227,7 +231,7 @@ private static string GetTypeAndHashSuffix(string exePath)
227
231
return suffix ;
228
232
}
229
233
230
- private void SetNamesAndVersion ( string applicationFilename , Assembly exeAssembly , bool isHttp )
234
+ private void SetNamesAndVersion ( Assembly exeAssembly , bool isHttp )
231
235
{
232
236
Type mainType = null ;
233
237
0 commit comments