Fetch file in multiple folder from a base folder, except the folder

Asked

Viewed 58 times

-2

string[] arquivos = Directory.GetFiles(@"C:\Teste_Zip\web", "*", SearchOption.AllDirectories);

The question is: I need to get the files except in a folder. I could not except the folder. The folder has Fixed name, does not vary.

I did this and says that it is not possible to convert char to string[]. The var files is a string array.

private void button1_Click(object sender, EventArgs e)
        {
            string[] result = Path.GetFileNameWithoutExtension(arquivos.ToString());

        }

All I need is to bring the name of the files and without the path of the files.

This way here, solved:

private void button1_Click(object sender, EventArgs e)
        {
            string s = string.Empty;
            List<string> lista = new List<string>();

            string path = ConfigurationSettings.AppSettings["Caminho_Base"];

            string[] arquivos = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            foreach(var item in arquivos)
            {
                s = Path.GetFileNameWithoutExtension(item);
                if (!item.Contains("TSNMVC"))
                    lista.Add(s);
            }

        }

2 answers

0

Make a condition using LINQ

var arquivos = Directory.GetFiles(@"C:\Teste_Zip\web", "*", SearchOption.AllDirectories)
.Where(x => x.StartsWith("Nome da pasta a excluir")).ToArray();
  • It didn’t work because arquivos in my context is files and need to delete folders.

  • True. Try using StartsWith(), shall produce the expected effect.

0

Just adjusting the jbueno code I believe works for what you need.

var files = Directory.GetFiles(@"C:\Teste_Zip\web", "*.*", SearchOption.AllDirectories)
.Where(d => !d.Contains("C:\Teste_Zip\PASTA_A_EXCLUIR")).ToArray();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.