Skip to content

Commit

Permalink
Fix to long path in Windows 10 and up.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmki committed Oct 22, 2022
1 parent 6d414c1 commit 9ede7f4
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 11 deletions.
9 changes: 6 additions & 3 deletions TTLAgent/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>
</configuration>
17 changes: 14 additions & 3 deletions TTLAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;
using Microsoft.Win32;
using SearchOption = System.IO.SearchOption;

namespace TTLAgent
Expand All @@ -18,7 +19,6 @@ class Program
{
private static bool _verbose;
private static bool _quiet;
private static bool _local;//direct database connection, no web service
private static bool _testMode;
private static bool _noquest;
private static bool _rmdir;
Expand All @@ -33,12 +33,14 @@ class Program
private static bool _ede;
private static int _fileCount;
private static int _dirCount;
private static bool _longPathsEnabled;
private static byte[] _bytez;//Used to overwrite files with bfg
private static int _keepalive = 0;//How many files to keep alive

//private const int _maxFiles = 1000;//Max files to delete at once
//private const int _maxDirs = 1000;//Max directories to delete at once
private const int _maxPath = 260;//Max path length
private const int _maxDirPathLength = 248;//Max directory path length

[STAThread()]
static void Main(string[] args)
Expand Down Expand Up @@ -147,6 +149,8 @@ static void Main(string[] args)
RecycleBinHelper.DeleteOldFiles(DateTime.Now.AddDays(_days * -1));
return;
}

CheckLongPathsEnabled();
//Process the file or directory
string targetName = args[0];
var isDir = IsDirectory(targetName);
Expand Down Expand Up @@ -176,13 +180,13 @@ static void Main(string[] args)

private static void ProcessFile(string path)
{
if (path.Length > _maxPath) return;
if (path.Length > _maxPath &! _longPathsEnabled) return;
bool result;
var fi = new FileInfo(path);
if (_days > 0)
{//Check if this file is too old
var fileDate = fi.CreationTime;
if (fi.LastWriteTime > fileDate) fileDate = fi.LastWriteTime;
if (fi.LastWriteTime > fileDate && fi.LastWriteTime < DateTime.Now) fileDate = fi.LastWriteTime;
if ((DateTime.Now - fileDate).TotalDays > _days)
{
result = DeleteFile(path);
Expand Down Expand Up @@ -282,6 +286,7 @@ private static bool DeleteFile(string path)

private static void FixFilePermissions(string path)
{
if (path.Length > _maxDirPathLength &! _longPathsEnabled) return;
FileAttributes fa = File.GetAttributes(path);
if ((fa & FileAttributes.ReadOnly) != 0)//ReadOnly file, fix it
{
Expand All @@ -297,6 +302,7 @@ private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAtt

private static void ProcessDirectory(string path)
{
if (path.Length > _maxDirPathLength &! _longPathsEnabled) return;
//1. Process all files in this directory
var files = new List<string>();
SearchOption so = SearchOption.TopDirectoryOnly;
Expand Down Expand Up @@ -385,6 +391,11 @@ private static void ProcessDirectory(string path)
return null;
}

private static void CheckLongPathsEnabled()
{
var lpe = Convert.ToInt32 (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem", "LongPathsEnabled", 0));
_longPathsEnabled = lpe > 0;
}
private static bool GetUserConsent(string prompt)
{
begin:
Expand Down
6 changes: 3 additions & 3 deletions TTLAgent/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Dmitry Kirsanov")]
[assembly: AssemblyProduct("TTLAgent")]
[assembly: AssemblyCopyright("Copyright ©2016 Dmitry Kirsanov http://kirsanov.net")]
[assembly: AssemblyCopyright("Copyright ©2016-2022 Dmitry Kirsanov http://kirsanov.net")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.3.2.*")]
[assembly: AssemblyFileVersion("3.3.2")]
[assembly: AssemblyVersion("3.4.0.*")]
[assembly: AssemblyFileVersion("3.4.0")]
2 changes: 1 addition & 1 deletion TTLAgent/Strings1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion TTLAgent/TTLAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TTLAgent</RootNamespace>
<AssemblyName>ttl</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<TargetFrameworkProfile />
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -36,6 +52,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="System" />
Expand All @@ -59,6 +78,7 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="app.manifest" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Strings.resx">
Expand All @@ -77,6 +97,18 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
79 changes: 79 additions & 0 deletions TTLAgent/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="3.4.0.0" name="TTL3"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />

<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />

<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

</application>
</compatibility>

<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>


<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->

</assembly>

0 comments on commit 9ede7f4

Please sign in to comment.