2015-07-21 89 views
0

我在这里的是用户从外部驱动器中选择一个文件夹完整的.txt文件和一个虚拟文件与文件名到本地文件夹。如何确保用户选择特定文件夹并从选定文件夹中的文件中删除文件扩展名?

我有两个问题关于下面的代码。

  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如果我正确地使用它来删除扩展,但林不知道。

任何帮助,将不胜感激。先谢谢你。

回答

2
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/"

+0

谢谢你的时间,但这没有为我工作。 –

+1

@Bigg_aye怎么这样?我想知道这样我可以解决我的答案。 (尤其是因为它为我工作。) – Jashaszun

+0

由于某种原因,它从未在现有文件夹中创建虚拟文件。该文件夹是一个现有的文件夹,所以我知道路径名,所以我删除了前三行,并按照您的建议在最后一行替换为“path /”的NEW_PATH。 –

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; 
+0

我很感激。你回答了我的第二个问题,但第一个问题是关于外部驱动器,所以当用户插入USB驱动器时,我无法预测驱动器将映射到USB驱动器。有没有一种方法来验证文件夹名称,因为无论usb映射到哪个驱动器,它都必须始终是相同的文件夹名称? –

+0

如果我对你有所了解,你可以从你已有的DirectoryInfo对象中获取文件夹名称。更新了答案。 – Equalsk

相关问题