Store the file name of all files in a directory into an array

zhaozj2021-02-16  64

// 9CBS is seen, put it here, have time to study ^ _ ^

Using system.collections; using system.io;

Namespace zz {class zztest {static void main () {string [] files = getfiles ("g: // back"); more (String file in file) {console.writeline (file);} console.readline (); } Public static string [] getfiles (string path) {arraylist files = new arraylist (); // Gets the file in the current directory FOREACH (String File in Directory.Getfiles (Files.Add (file);} / / Get the subdirectory file in the current directory Foreach (String Dir in Directory.GetDirectories (PATH)) {getFiles (Dir, Files);} return (String []) Files.toArray (TypeOf (String));} // Regeneration Function PUBLIC Static Void getFiles (String Path, ArrayList Files) {// Gets File (String File in Directory.Getfiles (PATH)) {files.add (file) {files.add (file)} // gets the child under the current directory. Directory File Foreach (String Dir in Directory.GetDirectories (PATH) {getFiles (DIR, Files);}}

}} ============================================= // C # traversal folder using system; use system. Data; using system.collections; using system.io; using system.net; using system.text; using system.diagnostics;

namespace ZZConsoleTest {public class ZZConsole {[STAThread] static void Main (string [] args) {string [] fileNameArray = GetFileNamesWithArray ( "D: // zhzuo // music"); foreach (string name in fileNameArray) {Console.WriteLine (Name);} console.writeline ("======================================== ========= "); ArrayList filenames = getFileNameSwitharrayList (" D: // zhzuo //rusic "); Foreach (Object Name In FileNameArray) {console.writeline (String) Name;} console.writeline ("Completed"); console.readline ();} // recursive method Private static void ListAllFiles (String SourceDir, arraylist al) {foreach (String filename in Directory.GetFiles) {al.add (filename);} foreach (string subDir in Directory.GetDirectories (sourceDir)) {ListAllFiles (subDir, al);}} // returns public static ArrayList GetFileNamesWithArrayList (string sourceDir) {ArrayList al = new ArrayList () with ArrayList embodiment; ListAllFiles (sourceDir, al ); Return al;} // Returns public S with String [] tatic string [] GetFileNamesWithArray (string sourceDir) {ArrayList al = new ArrayList (); ListAllFiles (sourceDir, al); return (string []) al.ToArray (typeof (string));}}} ====== ====================================== ///

/// will generate XML documents in the specified directory / // /// Root directory /// Returns an XMLDocument object <

/ Returns> public static XmlDocument CreateXml (string targetDir) {XmlDocument myDocument = new XmlDocument (); XmlDeclaration declaration = myDocument.CreateXmlDeclaration ( "1.0", "utf-8", null); myDocument.AppendChild (declaration); XmlElement rootElement = myDocument.CreateElement (targetDir.Substring (targetDir.LastIndexOf ( "//") 1)); myDocument.AppendChild (rootElement); foreach (string fileName in Directory.GetFiles (targetDir)) {XmlElement childElement = myDocument.CreateElement ( " File "); childElement.InnerText = fileName.Substring (fileName.LastIndexOf (" // ") 1); rootElement.AppendChild (childElement);} foreach (string directory in Directory.GetDirectories (targetDir)) {XmlElement childElement = myDocument .CreateElement ( "Directory"); childElement.SetAttribute ( "Name", directory.Substring (directory.LastIndexOf ( "//") 1)); rootElement.AppendChild (childElement); CreateBranch (directory, childElement, myDocument); } Return MyDocument;} ///

/// Generate XML branch /// /// Subdate /// Parent directory Xmldocument /// XMLDocument object Private static void Createbranch (String targetDir, XmlElement xmlNode, XmlDocument myDocument) {foreach (string fileName in Directory.GetFiles (targetDir)) {XmlElement childElement = myDocument.CreateElement ( "File"); childElement.InnerText = fileName.Substring (fileName.LastIndexOf ( "//" ) 1); XMLNode.Appendchild (ChildElement);

转载请注明原文地址:https://www.9cbs.com/read-17392.html

New Post(0)