简体   繁体   English

如何确保用户选择特定文件夹并从所选文件夹中的文件中删除文件扩展名?

[英]How to make sure user select specific folder and remove file extension from files in the selected folder?

What I have here is the user select a folder full of .txt files from a external drive and a dummy file is made with the filename to a local folder. 我在这里所要执行的操作是,用户从外部驱动器中选择一个充满.txt文件的文件夹,然后使用该文件名将伪文件复制到本地文件夹中。

I have 2 questions regarding the code below. 我对以下代码有2个问题。

  1. How do I verify that the user select a specific folder? 如何验证用户选择特定文件夹?
  2. How do I remove the .txt extension? 如何删除.txt扩展名? The code copies the file name and creates the files but its labeled "text.txt.png" but I need it to read "text.png". 该代码复制了文件名并创建了文件,但其标签为“ 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; } } } 

Note: I tried using Path.GetFileNameWithoutExtension to remove the extension but Im not sure if i was using it correctly. 注意:我尝试使用Path.GetFileNameWithoutExtension删除扩展名,但是我不确定我是否使用正确。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you in advance. 先感谢您。

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

You can use GetFileNameWithoutExtension , and it makes it pretty easy. 您可以使用GetFileNameWithoutExtension ,它非常容易。

Also, if you're already sure that your new folder exists, then you can elide the first three lines and replace NEW_PATH with "path/" in the last line. 另外,如果您已经确定新文件夹存在,则可以NEW_PATH前三行,并在最后一行NEW_PATH替换为"path/"

To see the returned path just use the SelectedPath property, then you can compare it to whatever you had in mind. 要查看返回的路径,只需使用SelectedPath属性,然后可以将其与您想到的内容进行比较。 The @ before the path just means I don't have to escape any characters, it's the same as "C:\\\\MyPath" . 路径前的@只是意味着我不必转义任何字符,它与"C:\\\\MyPath"

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

You already hit the nail on the head about the file name without extension, change this line: 您已经在文件名上打了钉,没有扩展名,请更改此行:

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

To this: 对此:

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

Edit: 编辑:

To get the folder name you already have the DirectoryInfo object so just use that: 要获取文件夹名称,您已经有了DirectoryInfo对象,因此只需使用它:

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

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

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