question

ElenaAndreea-4193 avatar image
0 Votes"
ElenaAndreea-4193 asked Castorix31 answered

SaveFileDialog save button is unresponsive when longfilepaths are used

Hello,
I'm using windows 10 and net 4.7.2.
I've to implement the following usecase. I want to restrict the users to select a filepath which is longer than the maximum size (260 characters), i don't want to force the users to manually enable the longpath option in registry, so for safety I want to use the FileOk event to check either is the filepath length is < 260 and the file name is valid. After that if the dialogresult is ok then i perform my operation. I have noticed that when using very long file paths the save button is not responsive and the fileok event is not fired . As a workaround for this -very strage - would be to set showHelp property to true in this case the fileok is fired but sometimes it throws the given file format is not supported.
Any help, workaround will be highly appreciated.


 using (SaveFileDialog exportDialog = new SaveFileDialog())
                 {
                     exportDialog.Title = "title";
                     exportDialog.CheckPathExists = false;
                     exportDialog.Filter = "Bak files (*.bak)|*.bak";
                     exportDialog.FileOk += CheckIfSelectedFileIsValid;
                     if (exportDialog.ShowDialog() == DialogResult.OK)
                     {
                         //handle some logic
                     }
    
                 }
                    
    
  private void CheckIfSelectedFileIsValid(object sender, CancelEventArgs e)
         {
             SaveFileDialog sv = (sender as SaveFileDialog);
             FileInfo file = new FileInfo(sv.FileName);
    
             if (IsExportFileLengthInvalid(file))
             {
                 CancelExportStatement(Constants_Errors.INVALID_EXPORT_DATABASE_FILE_PATH_TITLE, Constants_Errors.INVALID_EXPORT_DATABASE_FILE_PATH, e);
                 return;
             }
    
             if (IsExportFileNameInvalid(file))
             {
                 CancelExportStatement(Constants_Errors.INVALID_DATABASE_NAME_TITLE, Constants_Errors.INVALID_DATABASE_NAME,e);
             }
    
         }
            
           private bool IsExportFileLengthInvalid(FileInfo file)
         {
             const int MAX_FILE_LENGTH = 260;
                
             return Path.GetFullPath(file.Name).Length >= MAX_FILE_LENGTH;
         }
    
         private bool IsExportFileNameInvalid(FileInfo file)
         {
             var newExportConfigurationName = Path.GetFileNameWithoutExtension(file.Name);
             return string.IsNullOrWhiteSpace(newExportConfigurationName) || !char.IsLetter(newExportConfigurationName[0]) || !new TextChecks().IsNameValid(newExportConfigurationName);
         }
    
         private void CancelExportStatement(string title, string message, CancelEventArgs e)
         {
             RadMessageBox.Instance.StartPosition = FormStartPosition.CenterParent;
             DialogResult dResult = RadMessageBox.Show(message, title, MessageBoxButtons.OK, RadMessageIcon.Error);
    
             if (dResult == DialogResult.OK)
             {
                 e.Cancel = true;
             }


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered ElenaAndreea-4193 commented

In this thread SaveFileDialog does not allow long pathnames to be returned,
I had tested IFileSaveDialog which worked with long file paths
(I had posted a sample in C# in this other thread
(replace IFileOpenDialog and FileOpenDialog by IFileSaveDialog and FileSaveDialog in the class
change nOptions, remove "else if (hr == HRESULT.E_UNEXPECTED)" test)




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.