【问题标题】:How to make sure user select specific folder and remove file extension from files in the selected folder?如何确保用户选择特定文件夹并从所选文件夹中的文件中删除文件扩展名?
【发布时间】:2015-10-11 05:52:30
【问题描述】:

我在这里是用户从外部驱动器中选择一个充满 .txt 文件的文件夹,并使用文件名创建一个虚拟文件到本地文件夹。

我对下面的代码有 2 个问题。

  1. 如何验证用户是否选择了特定文件夹?
  2. 如何删除 .txt 扩展名?该代码复制文件名并创建文件,但其标记为“text.txt.png”,但我需要它来读取“text.png”。

        int g;
    
        private void folderSelect()
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.RootFolder = Environment.SpecialFolder.MyComputer;
            folder.ShowNewFolderButton = false;
            folder.Description = "Select Folder";
    
            if (folder.ShowDialog() == DialogResult.OK)
            {
                DirectoryInfo files = new DirectoryInfo(folder.SelectedPath);
                FileInfo[] textFiles = files.GetFiles("*.txt");
    
                while (g < textFiles.Length)
                {
    
                    if (g <= textFiles.Length)
                    {
                        File.Create("path/" + (textFiles[g].Name + ".png"));
                        g++;
                    }
                    else
                    {
                        break;
                    }
                }
    
            }
    

注意:我尝试使用 Path.GetFileNameWithoutExtension 删除扩展名,但我不确定我是否正确使用它。

任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:
    const string NEW_PATH = "path/";
    if (!Directory.Exists(NEW_PATH))
        Directory.CreateDirectory(NEW_PATH);
    const string PATH_TO_CHECK = "correctpath";
    
    FolderBrowserDialog folder = new FolderBrowserDialog();
    folder.RootFolder = Environment.SpecialFolder.MyComputer;
    folder.ShowNewFolderButton = false;
    folder.Description = "Select Folder";
    
    if (folder.ShowDialog() == DialogResult.OK)
    {
        string pathPastDrive = folder.SelectedPath.Substring(Path.GetPathRoot(folder.SelectedPath).Length).ToLower();
        // Here it depends on whether you want to check (1) that the path is EXACTLY what you want,
        // or (2) whether the selected path just needs to END in the path that you want.
        /*1*/ if (!pathPastDrive == PATH_TO_CHECK)
        /*2*/ if (!pathPastDrive.EndsWith(PATH_TO_CHECK))
            return; // or you can throw an exception if you want
    
        foreach (string textFile in Directory.GetFiles(folder.SelectedPath, "*.txt"))
            File.Create(NEW_PATH + Path.GetFileNameWithoutExtension(textFile) + ".png");
    }
    

    您可以使用GetFileNameWithoutExtension,这很容易。

    另外,如果您已经确定新文件夹存在,那么您可以省略前三行并将最后一行中的 NEW_PATH 替换为 "path/"

    【讨论】:

    • @Bigg_aye 怎么样?我想知道,以便我可以解决我的答案。 (特别是因为它对我有用。)
    • @Bigg_aye 另外,由于我不小心忽略了您的第一个问题,所以我只是回答了它。现在再次查看我的答案。
    • @Bigg_aye 我相信我确实回答了你的第一个问题,所以如果我没有回答,请告诉我你想让我澄清什么。在我的代码中,它确实确保所选路径是您正在寻找的特定路径,无论它位于哪个驱动器上。
    • @Bigg_aye 哦,糟糕,path 应该是 folder.SelectedPath。只是这部分是我单独写的。
    • @Bigg_aye 这就是我来这里的目的。祝你的项目在这里顺利!
    【解决方案2】:

    要查看返回的路径,只需使用SelectedPath 属性,然后您可以将其与您想到的任何内容进行比较。路径前的@ 只是表示我不必转义任何字符,它与"C:\\MyPath" 相同。

    FolderBrowserDialog folder = new FolderBrowserDialog();
    if (folder.ShowDialog() == DialogResult.OK)
    {
        if (folder.SelectedPath == @"C:\MyPath")
        {
            // DO SOMETHING
        }
    }
    

    您已经对不带扩展名的文件名一针见血,请更改此行:

    File.Create("path/" + (textFiles[g].Name + ".png"));
    

    到这里:

    File.Create("path/" + (Path.GetFileNameWithoutExtension(textFiles[g].Name) + ".png"));
    

    编辑:

    要获取文件夹名称,您已经拥有 DirectoryInfo 对象,因此只需使用它:

    DirectoryInfo files = new DirectoryInfo(folder.SelectedPath);
    string folderName = files.Name;
    

    【讨论】:

    • 我很感激。您回答了我的第二个问题,但第一个问题是关于外部驱动器的,因此当用户插入 USB 驱动器时,我无法预测哪个驱动器将映射到 USB 驱动器。有没有办法只验证文件夹名称,因为无论 USB 映射到什么驱动器,它都必须始终是相同的文件夹名称?
    • 如果我的理解正确,您可以从已有的 DirectoryInfo 对象中获取文件夹名称。更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多