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;
}