Skip to content

Commit b84d877

Browse files
committed
Add support for long file paths
1 parent 3836890 commit b84d877

File tree

6 files changed

+106
-8
lines changed

6 files changed

+106
-8
lines changed

App.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
55
</startup>
6+
<runtime>
7+
<!-- https://docs.microsoft.com/en-us/archive/blogs/jeremykuhne/net-4-6-2-and-long-paths-on-windows-10 -->
8+
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false"/>
9+
</runtime>
610
</configuration>

AsyncToSyncCodeRoundtripSynchroniserMonitorNet.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<PropertyGroup>
4242
<StartupObject>AsyncToSyncCodeRoundtripSynchroniserMonitor.Program</StartupObject>
4343
</PropertyGroup>
44+
<PropertyGroup>
45+
<ApplicationManifest>app.manifest</ApplicationManifest>
46+
</PropertyGroup>
4447
<ItemGroup>
4548
<Reference Include="Microsoft.Extensions.Configuration, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
4649
<HintPath>..\AsyncToSyncCodeRoundtripSynchroniserMonitor\packages\Microsoft.Extensions.Configuration.1.1.2\lib\netstandard1.1\Microsoft.Extensions.Configuration.dll</HintPath>
@@ -161,6 +164,7 @@
161164
</ItemGroup>
162165
<ItemGroup>
163166
<None Include="App.config" />
167+
<None Include="app.manifest" />
164168
<None Include="appsettings.example.json" />
165169
<None Include="appsettings.json">
166170
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

AsyncToSyncConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public static async Task AsyncFileUpdated(string fullName, Context context)
6565
{
6666
using (await Global.FileOperationAsyncLock.LockAsync())
6767
{
68-
var fileData = await FileExtensions.ReadAllTextAsync(fullName, context.Token);
68+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
69+
var fileData = await FileExtensions.ReadAllTextAsync(@"\\?\" + fullName, context.Token);
6970
var originalData = fileData;
7071

7172

Program.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ public static async Task FileUpdated(string fullName, Context context)
451451
}
452452
else //Assume ResX file
453453
{
454-
var fileData = await FileExtensions.ReadAllBytesAsync(fullName, context.Token);
454+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
455+
var fileData = await FileExtensions.ReadAllBytesAsync(@"\\?\" + fullName, context.Token);
455456
var originalData = fileData;
456457

457458
//save without transformations
@@ -705,8 +706,9 @@ public static async Task SaveFileModifications(string fullName, string fileData,
705706

706707

707708
//NB! detect whether the file actually changed
708-
var otherFileData = File.Exists(otherFullName)
709-
? await FileExtensions.ReadAllTextAsync(otherFullName, context.Token)
709+
var otherFileData = File.Exists(otherFullName)
710+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
711+
? await FileExtensions.ReadAllTextAsync(@"\\?\" + otherFullName, context.Token)
710712
: null;
711713

712714
if (
@@ -718,7 +720,8 @@ public static async Task SaveFileModifications(string fullName, string fileData,
718720

719721
Directory.CreateDirectory(Path.GetDirectoryName(otherFullName));
720722

721-
await FileExtensions.WriteAllTextAsync(otherFullName, fileData, context.Token);
723+
//@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
724+
await FileExtensions.WriteAllTextAsync(@"\\?\" + otherFullName, fileData, context.Token);
722725

723726
var now = DateTime.UtcNow; //NB! compute now after saving the file
724727
Global.ConverterSavedFileDates[otherFullName] = now;
@@ -751,7 +754,8 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
751754

752755
//NB! detect whether the file actually changed
753756
var otherFileData = File.Exists(otherFullName)
754-
? await FileExtensions.ReadAllBytesAsync(otherFullName, context.Token)
757+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
758+
? await FileExtensions.ReadAllBytesAsync(@"\\?\" + otherFullName, context.Token)
755759
: null;
756760

757761
if (
@@ -763,7 +767,8 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
763767

764768
Directory.CreateDirectory(Path.GetDirectoryName(otherFullName));
765769

766-
await FileExtensions.WriteAllBytesAsync(otherFullName, fileData, context.Token);
770+
//@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
771+
await FileExtensions.WriteAllBytesAsync(@"\\?\" + otherFullName, fileData, context.Token);
767772

768773
var now = DateTime.UtcNow; //NB! compute now after saving the file
769774
Global.ConverterSavedFileDates[otherFullName] = now;

SyncToAsyncConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static async Task SyncFileUpdated(string fullName, Context context)
6363
{
6464
using (await Global.FileOperationAsyncLock.LockAsync())
6565
{
66-
var fileData = await FileExtensions.ReadAllTextAsync(fullName, context.Token);
66+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
67+
var fileData = await FileExtensions.ReadAllTextAsync(@"\\?\" + fullName, context.Token);
6768
var originalData = fileData;
6869

6970

app.manifest

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC Manifest Options
8+
If you want to change the Windows User Account Control level replace the
9+
requestedExecutionLevel node with one of the following.
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
Specifying requestedExecutionLevel element will disable file and registry virtualization.
16+
Remove this element if your application requires this virtualization for backwards
17+
compatibility.
18+
-->
19+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- A list of the Windows versions that this application has been tested on
27+
and is designed to work with. Uncomment the appropriate elements
28+
and Windows will automatically select the most compatible environment. -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
35+
36+
<!-- Windows 8 -->
37+
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
38+
39+
<!-- Windows 8.1 -->
40+
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
41+
42+
<!-- Windows 10 -->
43+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
49+
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
50+
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
51+
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
52+
<!--
53+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
54+
<windowsSettings>
55+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
56+
</windowsSettings>
57+
</application>
58+
-->
59+
60+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
61+
<windowsSettings>
62+
<!-- https://docs.microsoft.com/en-us/archive/blogs/jeremykuhne/net-4-6-2-and-long-paths-on-windows-10 -->
63+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
64+
</windowsSettings>
65+
</application>
66+
67+
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
68+
<!--
69+
<dependency>
70+
<dependentAssembly>
71+
<assemblyIdentity
72+
type="win32"
73+
name="Microsoft.Windows.Common-Controls"
74+
version="6.0.0.0"
75+
processorArchitecture="*"
76+
publicKeyToken="6595b64144ccf1df"
77+
language="*"
78+
/>
79+
</dependentAssembly>
80+
</dependency>
81+
-->
82+
83+
</assembly>

0 commit comments

Comments
 (0)