How to verify if a file name has 16 digits?

Asked

Viewed 230 times

2

namespace _06_Teste
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdValidar_Click(object sender, EventArgs e)
        {
            foreach (String file in Directory.GetFiles("c:\\ImgRoute\\Caixa9 D1\\", "*.*"))
            {
                FileInfo fileName = new FileInfo(file);

                string imag = fileName.Name;

                if (imag.Length == 16)
                {
                    //("Numero do auto correto");
                }
                else
                {
                    //("Numero do auto invalido");
                }
            }
        }
    }
}
  • 6

    Armanda, I have edited your question so that I think it will be better. It would be necessary, however, for you to explain what is the problem you encounter with your code.

  • The code is not running. And I would like to know how best to display the error if the file name does not have 16 digits.

  • 2

    @Amanda you have to say what is going on. Is it wrong? Which one? Gives an unwanted result? Which one would be expected? Edit the question and give details, we can’t guess what your problem is. What’s the file name? Is it just the name without the extension? Com? With the path complete? When developing software we should be detailed.

2 answers

3

Appendage using at the beginning of the file:

using System.IO;

Example:

using System.IO;

namespace _06_Teste
{
    ......
    Directory.GetFiles(....)
    FileInfo fileName = new System.IO.FileInfo(file);

or call the function using the full path

System.IO.Directory.GetFiles(....)
System.IO.FileInfo fileName = new System.IO.FileInfo(file);
  • This code does nothing, the person wants to know if the file name has 16 digits this code nor takes the file name. The function Path.GetFileWithoutExtension(String) necessary to perform the task of returning the file name without the extension nor mentioned. The structure System.IO.FileInfo has the field Name which returns the file name with the extension.

  • 1

    At the time of the question, I noticed that the person was checking if (imag.Length == 16) , but had forgotten to put the using System.IO; I understood that by doing this, her code would work as she expected. Obviously the code needs to be improved. But the user did not give more return here.

2

I believe you are trying to get the file name without extension so you should use the method GetFileNameWithoutExtension(). I’ll edit it if you give me more details.

foreach (var file in Directory.GetFiles(@"c:\ImgRoute\Caixa9 D1\", "*.*")) {
    var imagem = Path.GetFileNameWithoutExtension(file);
    if (imagem.Length == 16) {
        //("Numero do auto correto");
    } else {
        //("Numero do auto invalido");
    }
}

I put in the Github for future reference.

If you want with the extension you must use the method GetFileName.

I’d rather use Directory.EnumerateFiles("c:\\ImgRoute\\Caixa9 D1\\", "*.*") if using . NET 4 on. It is faster.

Browser other questions tagged

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