简体   繁体   English

自定义检查SaveFileDialog上的文件名

[英]Custom checking file name on SaveFileDialog

I have a SaveFileDialog . 我有一个SaveFileDialog

When the user clicks on OK I have to check if there is a similar file name. 当用户点击OK时,我必须检查是否有类似的文件名。

The system has been doing such a test, but I need to add a test Is there a file with a similar name and numbering. 系统一直在做这样的测试,但我需要添加一个测试是否有一个类似名称和编号的文件。

For example, if the user selected a file name "a" and there is a file "a1" or "a2", warning message should appear. 例如,如果用户选择了文件名“a”并且存在文件“a1”或“a2”,则应出现警告消息。 (as it appears when there is a file named "a"). (当有一个名为“a”的文件时出现)。

Is there a way to do this? 有没有办法做到这一点?

SaveFileDialog inherits FileDialog class which has FileOk event. SaveFileDialog继承具有FileOk事件的FileDialog类。 You can put logic to check if similar files already exist in the handler method for this event. 您可以使用逻辑来检查此事件的处理程序方法中是否已存在类似文件。 If the result is true , display warning message. 如果结果为true ,则显示警告消息。 Then if user choose No from the warning dialog, set Cancel property of CancelEventArgs parameter to True , this will prevent save file dialog window from closing : 然后,如果用户从警告对话框中选择No ,则将CancelEventArgs参数的Cancel属性设置为True ,这将阻止关闭保存文件对话框窗口:

var dlg = new SaveFileDialog();
dlg.FileOk += (o, args) =>
              {
                  var file = dlg.FileName;
                  if (isSimilarFileExist(file))
                  {
                      var result = MessageBox.Show("Similar file names exist in the same folder. Do you want to continue?", 
                                                    "Some dialog title", 
                                                    MessageBoxButtons.YesNo, 
                                                    MessageBoxIcon.Warning
                                                  );
                      if(result == DialogResult.No)
                        args.Cancel = true;
                  }
              };
dlg.ShowDialog();

......

private bool isSimilarFileExist(string file)
{
    //put your logic here
}

this is the answer you want 这就是你想要的答案

SaveFileDialog S = new SaveFileDialog();
if(S.ShowDialog() == DialogResult.OK)
{
    bool ShowWarning = false;
    string DirPath = System.IO.Path.GetDirectoryName(S.FileName);
    string[] Files = System.IO.Directory.GetFiles(DirPath);
    string NOFWE = DirPath+"\\"+System.IO.Path.GetFileNameWithoutExtension(S.FileName);
    foreach (var item in Files)
    {

        if (item.Length > NOFWE.Length && item.Substring(0, NOFWE.Length) == NOFWE)
        {
            int n;
            string Extension = System.IO.Path.GetExtension(item);
            string RemainString = item.Substring(NOFWE.Length, item.Length - Extension.Length - NOFWE.Length);
            bool isNumeric = int.TryParse(RemainString, out n);
            if(isNumeric)
            {
                ShowWarning = true;
                break;
            }

        }
    }
    if(ShowWarning)
    {
        if (MessageBox.Show("Warning alert!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            Save();//Saving instance
    }
    else
    {
        Save();//Saving instance
    }
}

ans Save() method is the saving instructions... ans Save()方法是保存说明...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM