How do I prevent the file I created in txt to record two equal C#?

Asked

Viewed 93 times

1

People to better specify my problem, it is like this: I have a system that performs barcode reading and, in this precise system that the txt file that is receiving the code does not receive duplicate/repeated files, IE, it can not receive the same code 2 times.

I’m having problems doing this without a database and because the windows platform where this system will run is very old I have to program on VS2008.

Below is a part of the code, where the file Count.txt is created by the system and the file receives the barcode and the collection location:

if (!File.Exists(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Contagem.txt"))
{
    File.Create(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Contagem.txt").Close();
}
StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Contagem.txt", true);

sw.WriteLine("|" + txtCodBarras.Text + "|" + txtLocal.Text.Substring(txtLocal.Text.Length - 3) + "|");

sw.Close();
sw.Dispose();
  • you will have to read the entire file, and check if the code already exists in it

  • 2

    But I need the software to do it alone.

  • put the code you already have ready, then we assist you with examples

  • I will edit the post

  • to better specify your problem, include your code

  • Ready I’ve made the edit

Show 1 more comment

2 answers

2

Leandro Angelo’s answer gives a path that solves well, but has a problem of running condition. Do not try to check if the file exists, try to use it if it gives problem deal with the error. It is a complicated problem because it will almost always work, mainly in tests and will give the impression that there is no problem, the day that gives problem, will not have the slightest idea of what happened, even because it will not be able to reproduce.

A example with path, is the same thing.

1


If your goal is to avoid duplicating a record you should first read the file and check if it no longer has that record before writing the new one

   string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Contagem.txt";

    if (!File.Exists(path))
    {
        File.Create(path).Close();
    }


    bool existeRegistro = false;

    using (StreamReader reader = new StreamReader(path))
    {
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            if (line.Contains(txtCodBarras.Text))
                existeRegistro = true;

            if (existeRegistro)
                break;
        }
    }

    if (!existeRegistro)
    {

        using (StreamWriter sw = new StreamWriter(path, true))
        {
            sw.WriteLine("|" + txtCodBarras.Text + "|" + txtLocal.Text.Substring(txtLocal.Text.Length - 3) + "|");
        };

    }

Browser other questions tagged

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