繁体   English   中英

我需要帮助使用 C# 为所选目录文件夹中的每个文件获取(主子文件夹)

[英]I need help getting (Main Subfolder) For Each File in selected directory folder using C#

我的申请图片

我正在尝试获取文件夹和子文件夹中的所有文件并获取它的所有信息,但我在获取主文件夹名称和路径(祖父或祖父或父母的祖父取决于有多少子文件夹)时感到困惑包含文件的子文件夹

我需要为不同的子文件夹级别获取主容器文件夹

(----------------1--------------)
Main Folder For File            listBox6.Items.Add(?????????);

-Main Folder
-Sub 1
-Sub 1
-Sub 2
-Sub 3
-Sub 4
-Sub 4
-Sub 5
-Sub 5
-Sub 6
-Sub 7

(----------------2--------------)
Path For Main Folder           listBox7.Items.Add(?????????);

-C:\Need Help\Main Folder
-C:\Need Help\Main Folder\Sub 1
-C:\Need Help\Main Folder\Sub 1
-C:\Need Help\Main Folder\Sub 2
-C:\Need Help\Main Folder\Sub 3
-C:\Need Help\Main Folder\Sub 4
-C:\Need Help\Main Folder\Sub 4
-C:\Need Help\Main Folder\Sub 5
-C:\Need Help\Main Folder\Sub 5
-C:\Need Help\Main Folder\Sub 6
-C:\Need Help\Main Folder\Sub 7

我的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Rename_Folders_Files__Subfolders_Subfiles_Move_copy
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();

            if (FBD.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                listBox2.Items.Clear();
                listBox3.Items.Clear();
                listBox4.Items.Clear();
                listBox5.Items.Clear();
                listBox6.Items.Clear();
                listBox7.Items.Clear();

                textBox1.Text = FBD.SelectedPath;

                String directoryName = FBD.SelectedPath;
                DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
                if (dirInfo.Exists == false)
                Directory.CreateDirectory(directoryName);

                List<String> MyMusicFiles = Directory.GetFiles(textBox1.Text, "*.max", SearchOption.AllDirectories).ToList();     // get all files from folders and subfolders

                foreach (string file in MyMusicFiles)
                {
                    FileInfo mFile = new FileInfo(file);
                    DirectoryInfo directoryInfo = new DirectoryInfo(mFile.DirectoryName);
                    int fCount = Directory.GetFiles(mFile.Directory.FullName, "*.max", SearchOption.TopDirectoryOnly).Length;       /// files count inside each folder

                    if (directoryInfo.Parent != null)
                    {
                        string up2 = directoryInfo.Parent.ToString();
                        listBox4.Items.Add(fCount);
                        // listBox1.Items.Add(mFile.Directory.FullName);
                        listBox2.Items.Add(mFile.Name);
                        listBox3.Items.Add(Path.GetFileName(mFile.DirectoryName));
                        listBox1.Items.Add(Path.Combine(mFile.DirectoryName, mFile.Name));
                        listBox5.Items.Add(Directory.GetParent(mFile.FullName).Parent);
                    }
                    else
                    {
                        string up2 = directoryInfo.Root.ToString().Split(Path.DirectorySeparatorChar)[2];
                        listBox5.Items.Add(up2);
                    }
                }
                label3.Text = listBox1.Items.Count.ToString();
                label4.Text = listBox2.Items.Count.ToString();
                label8.Text = listBox3.Items.Count.ToString();
                label9.Text = listBox5.Items.Count.ToString();
             }
        }

        public string get_parent_dir_path(string path)
        {
            // notice that i used two separators windows style "\\" and linux "/" (for bad formed paths)
            // We make sure to remove extra unneeded characters.
            int index = path.Trim('/', '\\').LastIndexOfAny(new char[] { '\\', '/' });

            // now if index is >= 0 that means we have at least one parent directory, otherwise the given path is the root most.
            if (index >= 0)
                return path.Remove(index);
            else
                return "";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value1 = listBox1.SelectedItem.ToString();
            textBox1.Text = get_parent_dir_path(value1);
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        }
    }
}

您可以使用Path.GetRelativePath方法获取相对于主容器文件夹父级的路径。

static string[] files = {
    @"C:\Need Help\Main Folder\File 1.max",
    @"C:\Need Help\Main Folder\Sub 1\File 2.max",
    @"C:\Need Help\Main Folder\Sub 1\Sub 8\Sub 9\Sub lO\File 3.max",
    @"C:\Need Help\Main Folder\Sub 2\Sub ll\File 4.max",
    @"C:\Need Help\Main Folder\Sub 3\Sub 12\File 5.max",
    @"C:\Need Help\Main Folder\Sub 4\Sub 13\File 6.max",
    @"C:\Need Help\Main Folder\Sub 4\Sub 13\File 7.max",
    @"C:\Need Help\Main Folder\Sub 5\Sub 14\File 8.max",
    @"C:\Need Help\Main Folder\Sub 5\Sub 14\Sub 15\File 9.max",
    @"C:\Need Help\Main Folder\Sub 6\Sub 16\Sub 17\File 10.max",
    @"C:\Need Help\Main Folder\Sub 7\Sub 18\File 11.max"
};

public static void Test()
{
    string mainFolder = @"C:\Need Help\Main Folder";
    foreach (string file in files) {
        string fileName = Path.GetFileName(file);
        string folder = Path.GetDirectoryName(file);
        string mainSubFolder = Path.GetRelativePath(mainFolder, folder);
        if (mainSubFolder == ".") { // We are in the main container folder.
            // Get last part of main container folder.
            mainSubFolder = Path.GetFileName(mainFolder);
        } else {
            // Get first part of the subfolder path.
            int i = mainSubFolder.IndexOf('\\');
            if (i >= 0) {
                mainSubFolder = mainSubFolder.Substring(0, i);
            }
        }
        Console.WriteLine($"Full path:    {file}");
        Console.WriteLine($"Main folder:  {mainSubFolder, -15} File name:  {fileName}");
        Console.WriteLine();
    }
}

印刷:

Full path:    C:\Need Help\Main Folder\File 1.max
Main folder:  Main Folder     File name:  File 1.max

Full path:    C:\Need Help\Main Folder\Sub 1\File 2.max
Main folder:  Sub 1           File name:  File 2.max

Full path:    C:\Need Help\Main Folder\Sub 1\Sub 8\Sub 9\Sub lO\File 3.max
Main folder:  Sub 1           File name:  File 3.max

Full path:    C:\Need Help\Main Folder\Sub 2\Sub ll\File 4.max
Main folder:  Sub 2           File name:  File 4.max

Full path:    C:\Need Help\Main Folder\Sub 3\Sub 12\File 5.max
Main folder:  Sub 3           File name:  File 5.max

Full path:    C:\Need Help\Main Folder\Sub 4\Sub 13\File 6.max
Main folder:  Sub 4           File name:  File 6.max

Full path:    C:\Need Help\Main Folder\Sub 4\Sub 13\File 7.max
Main folder:  Sub 4           File name:  File 7.max

Full path:    C:\Need Help\Main Folder\Sub 5\Sub 14\File 8.max
Main folder:  Sub 5           File name:  File 8.max

Full path:    C:\Need Help\Main Folder\Sub 5\Sub 14\Sub 15\File 9.max
Main folder:  Sub 5           File name:  File 9.max

Full path:    C:\Need Help\Main Folder\Sub 6\Sub 16\Sub 17\File 10.max
Main folder:  Sub 6           File name:  File 10.max

Full path:    C:\Need Help\Main Folder\Sub 7\Sub 18\File 11.max
Main folder:  Sub 7           File name:  File 11.max

更新

对于没有Path.GetRelativePath方法的 .NET 框架版本,您可以使用此方法:

string mainFolder = @"C:\Need Help\Main Folder";
foreach (string file in files) {
    string fileName = Path.GetFileName(file);
    string folder = Path.GetDirectoryName(file);
    string mainSubFolder;
    if (folder == mainFolder) { // We are in the main container folder.
        // Get last part of main container folder.
        mainSubFolder = Path.GetFileName(mainFolder);
    } else {
        mainSubFolder = folder.Substring(mainFolder.Length + 1); // + 1 for the "\".
        // Get first part of the subfolder path.
        int i = mainSubFolder.IndexOf('\\');
        if (i >= 0) {
            mainSubFolder = mainSubFolder.Substring(0, i);
        }
    }
    Console.WriteLine($"Full path:    {file}");
    Console.WriteLine($"Main folder:  {mainSubFolder,-15} File name:  {fileName}");
    Console.WriteLine();
}

暂无
暂无

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

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