Search code examples
c#wpfopenfiledialog

C# OpenFileDialog not returning right path if path length is above 256


I am coding some small application. The application will make some file name changes depending on some selections. To open the files I am using the Microsoft.Win32.OpenFileDialog. Now when I want to open a file with an path longer than 256 characters (I think thats the number) the path gets cut down. For example I have the following code to open the files:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.ValidateNames = false;

if (true == openFileDialog.ShowDialog())
{
    //Working with the returned path
}

Then when I want to open a file like this: C:/a/very/long/file/path/with/more/than/256/characters.txt, the path I get out of the dialog is something like this: C:/a/very/long/file/path/with/more/than/256/CHARAC~1.TXT The files name tho is 230 characters long.

How can I solve this problem?

Thanks for any help.


Solution

  • As noted in the comments, here is a summary of Peregrine's link.

    1. Create a new .reg file.
    2. Copy the following content into the file:
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
    "LongPathsEnabled"=dword:00000001
    
    1. Save the file and execute it.
    2. Right click on your project and add a new manifest file if it does not already exist.
    3. Open the app.manifest file and add the following:
    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
            <ws2:longPathAware>true</ws2:longPathAware>
        </windowsSettings>
    </application>