C#

April 16, 2005 - anibal_k @ 1:16 pm

Ayer me dijeron que necesitaban un script que crease un fichero xml reconociendo los ficheros (imagenes) que hay en un directorio. Como queria hacer algo en c# esta mañana he hecho esto. Es una primera version, que habria que modificar un poco, pero algo es algo.


// project created on 16/04/2005 at 12:06
using System;
using System.Xml;
using System.IO;

public class Explorador_Directorio
{
private string Folder_Path; //al final no he usado esta variable ^_^

public Explorador_Directorio (string Folder_Path)
{
this.Folder_Path = Folder_Path;
}
public void DisplayFileInfo(string fileFullName, XmlTextWriter tw)
{
FileInfo TheFile = new FileInfo(fileFullName);
if (!TheFile.Exists)
throw new FileNotFoundException("File not found: "
+ fileFullName);
// escribimos en el xml el objeto de la imagen
tw.WriteStartElement("imagen");
tw.WriteElementString("name", TheFile.Name);
tw.WriteElementString("descripcion", "No hay descripcion");
tw.WriteElementString("tag", "No tag");
tw.WriteEndElement();
}

public void DisplayFolderList(string folderFullName)
{
DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
if (!TheFolder.Exists)
throw new DirectoryNotFoundException("Folder not found: "
+ folderFullName);

//creamos fichero xml
XmlTextWriter tw = new XmlTextWriter(folderFullName + "images.xml", null);
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("carpeta"); //dentro de una carpeta todas las images
tw.WriteAttributeString("name", TheFolder.Name);

//listamos los directorios recursivamente
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
DisplayFolderList(folderFullName + NextFolder.Name);

//listamos todos los ficheros de la carpeta
foreach(FileInfo NextFile in TheFolder.GetFiles())
DisplayFileInfo (folderFullName + NextFile.Name, tw);

tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
//cerramos bien cerrados
}
}

class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Img_to_XML v0.1");

Explorador_Directorio ed = new Explorador_Directorio("/home/lucifer/Img_to_XML/img/");
ed.DisplayFolderList("/home/lucifer/Img_to_XML/img/");
}
}

Tal vez a alguien le sirva.
Para usar con mono:

mcs Img_to_XML.cs
mono Img_to_XML.exe

Ya pondre las modificaciones ;)
PD: no me respeta la tabulacion el editor este, lo siento

3 Comentarios »

  1. Solo tengo una pregunta… que host te has puesto en la ubuntu que te has puesto de user lucifer? xD

    Comment by _chachi_ — April 16, 2005 @ 2:07 pm

  2. por si acaso lo voy a explicar (no soy satanico, me parece una gran gilipollez que solo vale para reirse de los catolicos)

    LUC(ia)IFER(min) - :D

    Comment by anibal_k — April 16, 2005 @ 2:27 pm

  3. Joer… y pensar que mientras hacias eso otros estaban jugando al wow…

    Jeje gracias tio, voy a probarlo y te cuento…

    Comment by EmBiaSSuS — April 16, 2005 @ 8:04 pm

Comenta lo que quieras


Medida Anti-spam