从完整文件路径c#获取文件夹名称
Get folder name from full file path c#
我正在尝试找出文件夹的完整目录。例如Folder\TopLevel\Sub-Directory 及以后。但是,顶级文件夹的命名方式完全不同,例如顶级 1、顶级 2.. 它们都有不同的子目录等。即使所有文件夹的名称都不同,我如何才能一次显示所有目录。谢谢!
我的代码:
using System;
using System.IO;
namespace FolderStructure
{
public class Folder
{
static void Main(string[] args)
{
string[] fileArray = Directory.GetDirectories("H:\Folder\TopLevel\Sub-Directory\");
for (int i = 0; i < fileArray.Length; i++)
{
Console.WriteLine(fileArray[i]);
}
Console.ReadLine();
}
}
}
编辑:
此图显示了它如何只显示到子文件夹。但是,我想知道子文件夹内及以后的内容。
这是我几周前写的一个程序,它将结果保存到 xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}
我正在尝试找出文件夹的完整目录。例如Folder\TopLevel\Sub-Directory 及以后。但是,顶级文件夹的命名方式完全不同,例如顶级 1、顶级 2.. 它们都有不同的子目录等。即使所有文件夹的名称都不同,我如何才能一次显示所有目录。谢谢!
我的代码:
using System;
using System.IO;
namespace FolderStructure
{
public class Folder
{
static void Main(string[] args)
{
string[] fileArray = Directory.GetDirectories("H:\Folder\TopLevel\Sub-Directory\");
for (int i = 0; i < fileArray.Length; i++)
{
Console.WriteLine(fileArray[i]);
}
Console.ReadLine();
}
}
}
编辑:
此图显示了它如何只显示到子文件夹。但是,我想知道子文件夹内及以后的内容。
这是我几周前写的一个程序,它将结果保存到 xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}