【问题标题】:Check if a folder exists with only a part of the name in C#在 C# 中检查是否存在仅具有部分名称的文件夹
【发布时间】:2021-10-25 10:56:26
【问题描述】:

我创建了一个代码来创建带有两个文本框的文件夹。

  • Textbox1 - 客户编号 (XXXX)。
  • Textbox2 - 客户名称。

我希望能够在创建文件夹之前检查客户编号是否存在。 新创建的文件夹将是两个文本框的组合(这已经解决了)。 我只需要能够确定该文件夹是否仅存在客户编号,因为它可能是使用(客户编号 + 客户名称)创建的。

当前工作代码:

    {
        string no = textBox1.Text;
        string client = textBox2.Text;
        string carpeta = @"C:\" + no + " " + client;
        string sourcePath = @"C:\main";
        string destinationPath = @"C:\" + no + " " + client;
        textBox1.Clear();
        textBox2.Clear();

        try
        {
            
                if (Directory.Exists(carpeta))
                {
                    DialogResult y;
                    y = MessageBox.Show("Folder already exists\nDo you want to open it?", "AE.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (y == DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(@"C:\" + no + " " + client);
                    }
                    else
                    {
                        Close();
                    }
                }
                else
                {
                    DialogResult x;
                    x = MessageBox.Show("The folder doesn't exist\nWant to create a folder?." + "\n" + no + " " + client, "AE.", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (x == DialogResult.Yes)
                    {
                        Directory.CreateDirectory(carpeta);
                        FileSystem.CopyDirectory(sourcePath, destinationPath, UIOption.AllDialogs);
                        System.Diagnostics.Process.Start(@"C:\" + no + " " + client);
                    }
                    else
                    {
                        Close();
                    }
                }              
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:" + ex.Message);
        }
    }

【问题讨论】:

标签: c#


【解决方案1】:

您也可以每次需要该文件夹时都这样做:

    public static void Main()
    {
        var username = "someuser";
        var usernumber = "ABC123";
        var mainDirectory = @"C:\Path\To\The\Main\Dir";
        var pathToTheUserDirectory = Path.Combine(mainDirectory, $"{username}-{usernumber}");

        // This line will create the directory if not exist or take the existing directory.
        var directoryInfo = Directory.CreateDirectory(pathToTheUserDirectory);

        var directoryPath = directoryInfo.FullName;

        // ...  
        // or
        // directoryInfo.Delete(recursive: true);
    }

【讨论】:

    【解决方案2】:
    string[] dirs = Directory.GetDirectories(@"c:\",  txtTextBox.Text + "*");
    

    这只会获取以所需文本开头的目录

    编辑:如果客户编号有固定位置(例如 0000-9999 中的 4 个),这只是一个很好的解决方案

    Microsoft Documentation - check example below

    【讨论】:

    • txtTextBox.Text + "*" 不好。如果txtTextBox.Text为1,比如会匹配“1名”、“123名”、“1534653名”等。
    • 如果数字没有固定格式,那是正确的。在给出的示例中,它应该是 4 个位置 (0000-9999)。如果不是这种情况,则必须解析结果 - 然后可以使用另一个/更好的解决方案
    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2021-10-30
    • 2020-07-01
    • 2023-04-04
    • 2015-07-24
    • 2015-11-19
    • 2020-08-20
    • 2013-01-19
    相关资源
    最近更新 更多