Crear y borrar carpetas y archivos automáticamente con C# en Visual Studio

 



En este video te muestro como crear y borrar carpetas y archivos con C# de manera automática

Código 

-----------------------------------------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace crearCarpeta

{

    public partial class Form1 : Form

    {

           //Ruta del directorio

            string path = @"C:\Users\";

            string listaArchivos;

            string nombreArchivo;

            string nombreCarpeta;

            string eliminarArchivo;

       



        public Form1()

        {

            

          

            InitializeComponent();

            

            txtPath.Text = path;

            txtCarpeta.Text = "Carpeta1";

            txtArchivo.Text = "Archivo.txt";


            obtenerListaArchivos();

            

        }




        public void crearCarpeta() {


            path = txtPath.Text;

            nombreCarpeta = txtCarpeta.Text; ;


            //Validamos si existe

            if (Directory.Exists(path+nombreCarpeta))

            {

               //No hacemos nada


            }

            else

            {

                try

                {

                    //Creamos la carpeta

                    Directory.CreateDirectory(path + nombreCarpeta);

                }

                catch (Exception e) 

                

                {


                    MessageBox.Show("Error:"+path+nombreCarpeta+" --- "+e);

                

                

                }


            }


          

        }



        public void creaArchivo() {


            path = txtPath.Text;

            nombreArchivo = txtArchivo.Text;

            nombreCarpeta = txtCarpeta.Text;


            try

            {

                //Crear el archivo

                File.Create(path+nombreCarpeta+ "\\" + nombreArchivo);

                obtenerListaArchivos();

               

            }

            catch (Exception e) { 

            

                MessageBox.Show(e.Message);

            

            }


        }



        public void eliminaArchivo()

        {

            path = txtPath.Text;

            nombreCarpeta = txtCarpeta.Text;

            eliminarArchivo = txtEliminaArchivo.Text;


            try

            {

                //Eliminar archivo

                File.Delete(path+nombreCarpeta+eliminarArchivo);

                obtenerListaArchivos();

            }

            catch (Exception e) {


                MessageBox.Show(e.Message);

            

            }



        }


        public void obtenerListaArchivos() {


            richTextBox1.Clear();

            listaArchivos = null;

            path = txtPath.Text;

            nombreCarpeta = txtCarpeta.Text;


            DirectoryInfo carpeta = new DirectoryInfo(path+nombreCarpeta);

            FileInfo[] Archivos = carpeta.GetFiles();


            foreach (FileInfo archivo in Archivos)

            {

               listaArchivos += archivo.Name + "\r\n";


            }


            richTextBox1.Text = listaArchivos;



        }




        private void btnCrearCarpeta_Click(object sender, EventArgs e)

        {

            creaArchivo();


        }


        private void button1_Click(object sender, EventArgs e)

        {

            eliminaArchivo();

        }


        private void btnCrearCarpeta_Click_1(object sender, EventArgs e)

        {

            crearCarpeta();

        }


        private void textBox1_TextChanged(object sender, EventArgs e)

        {


        }

    }

}


---------------------------------------------------------------------------------------------------------------------

Comentarios

Lo mejor de Pixels Price

CREA TU PROPIO TEAM VIEWER CON C# (PARTE 3) CONTROLA EL TECLADO

Crear códigos de barras con C#

Crear documentos PDF con C#