blob: 7d535c5de776bbbedca7d7263495f568d7dd336e (
plain)
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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
namespace System
{
public static partial class AppContext
{
/// <summary>
/// Return the directory of the executable image for the current process
/// as the default value for AppContext.BaseDirectory
/// </summary>
private static string GetBaseDirectoryCore()
{
StringBuilder buffer = new StringBuilder(Interop.mincore.MAX_PATH);
while (true)
{
int size = Interop.mincore.GetModuleFileName(IntPtr.Zero, buffer, buffer.Capacity);
if (size == 0)
{
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
}
if (Marshal.GetLastWin32Error() == Interop.mincore.ERROR_INSUFFICIENT_BUFFER)
{
// Enlarge the buffer and try again.
buffer.EnsureCapacity(buffer.Capacity * 2);
continue;
}
// Return path to the executable image including the terminating slash
string fileName = buffer.ToString();
return fileName.Substring(0, fileName.LastIndexOf('\\') + 1);
}
}
}
}
|