Click here to Skip to main content
15,793,018 members
Home / Discussions / C#
   

C#

 
AnswerRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
Richard Deeming16-Aug-23 4:11
mveRichard Deeming16-Aug-23 4:11 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
chris_brabant16-Aug-23 5:00
chris_brabant16-Aug-23 5:00 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
Richard Deeming16-Aug-23 5:14
mveRichard Deeming16-Aug-23 5:14 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
chris_brabant16-Aug-23 5:48
chris_brabant16-Aug-23 5:48 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
Richard Deeming16-Aug-23 22:14
mveRichard Deeming16-Aug-23 22:14 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
chris_brabant16-Aug-23 22:33
chris_brabant16-Aug-23 22:33 
GeneralRe: File csproj : how to create a nuget package with msbuild and with package dependencies ? Pin
Richard Deeming16-Aug-23 22:52
mveRichard Deeming16-Aug-23 22:52 
Questionhow to edit and delete lines from a text file Pin
Edilson Lemos 202112-Aug-23 13:36
Edilson Lemos 202112-Aug-23 13:36 
Hello Colleagues, I would like to take a doubt!
Personal apologies if this is something I could google, but as you know there are a lot of articles with no opinion on the subject, I spent days reading and got nowhere.
I prefer your opinion...

I recently changed my project, disconnected from the database and saved it in a Text file, as it was just a table and nothing confidential, I solved it this way, because I think that when installing it will make the process easier, but a doubt arose in me on how to edit, and delete just one line from the file following the Contest column which is the first column,

-Question: Is it possible to edit only a certain line in a text file with more than two thousand lines?
Is it possible to delete a particular row tabem?

if it is possible: what would be the best way (Indication),
If not, what would be the best way?
database or what other way?

I have a form like in the image where I can type the numbers and save
Exemple image

my Save button and your Save TXT method
C#
private void salvarTXT()
        {
            if (!string.IsNullOrWhiteSpace(txtConcurso.Text))
            {
                resultUltimat();
                MessageBox.Show("Texto salvo com sucesso!");
            }
            else
            {
                MessageBox.Show("Insira um Resultado para salvar no arquivo Texto!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

 private void resultUltimat()// salva lista de numeros do sorteio (Resultado)  // save list of raffle numbers (Result)
        {
            // salva ultimo sorteio no arquivo texto na primeira linha e vai acrescentando linhas
            // saves the last draw in the text file on the first line and adds lines
            const string nomeArquivo = @"C:\BoaSorte\Banco\Resultados.txt";
            List<string> linhas = File.ReadLines(nomeArquivo).ToList(); // Passo 1

            if (linhas.IndexOf(txtConcurso.Text + "," + ResultTextBox[0].Text + "," + ResultTextBox[1].Text + "," + ResultTextBox[2].Text + "," + ResultTextBox[3].Text + "," + ResultTextBox[4].Text
               + "," + ResultTextBox[5].Text + "," + ResultTextBox[6].Text + "," + ResultTextBox[7].Text + "," + ResultTextBox[8].Text + "," + ResultTextBox[9].Text
               + "," + ResultTextBox[10].Text + "," + ResultTextBox[11].Text + "," + ResultTextBox[12].Text + "," + ResultTextBox[13].Text + "," + ResultTextBox[14].Text) >= 0);
            //Element found in list.
            //MessageBox.Show("Resultado já Exists no arquivo de texto!");
            else
            {
                linhas.Insert(0, txtConcurso.Text + "," + ResultTextBox[0].Text + "," + ResultTextBox[1].Text + "," + ResultTextBox[2].Text + "," + ResultTextBox[3].Text + "," + ResultTextBox[4].Text
             + "," + ResultTextBox[5].Text + "," + ResultTextBox[6].Text + "," + ResultTextBox[7].Text + "," + ResultTextBox[8].Text + "," + ResultTextBox[9].Text
             + "," + ResultTextBox[10].Text + "," + ResultTextBox[11].Text + "," + ResultTextBox[12].Text + "," + ResultTextBox[13].Text + "," + ResultTextBox[14].Text); // Passo 2
                File.WriteAllLines(nomeArquivo, linhas);
                //MessageBox.Show("Gravndo!");
            }
        }

my text file is being saved like this
[Exemple Image]
How I tried the edit method
C#
private void editar()
        {
            try
            {
                using (StreamReader lendo = new StreamReader(@"C:\BoaSorte\Banco\Resultados.txt"))
                {
                    while (lendo.Peek() != -1)
                    {
                        int linha = File.ReadAllLines(@"C:\BoaSorte\Banco\Resultados.txt").GetLength(0);

                        for (int i = 1; i <= linha; i++)
                        {
                            if (lendo.ReadLine() == txtConcurso.Text)
                            {
                                string caminhoArquivo = @"C:\BoaSorte\Banco\Resultados.txt";

                                //Numero da linha que o conteúdo vai ser alterado
                                // Line number where the content will be changed

                                //Lendo arquivo e atribuindo em um array de string
                                //Reading file and assigning it to a string array

                                string[] arquivo = File.ReadAllLines(caminhoArquivo);

                                //Mudando o valor da linha informada
                                // Changing the value of the informed line

                               // arquivo = ResultTextBox[i].Text; //here is the error
                                lendo.Close();
                                //gravando o conteúdo por cima do arquivo,porem trava nessa linha falando que ja esta em uso
                                // recording the content over the file, but it hangs on this line saying that it is already in use
                                System.IO.File.WriteAllLines(caminhoArquivo, arquivo);
                            }
                        }
                    }
                }
            }

            catch (Exception)

            {

            }

or like this
C#
private void ChangeUser(string currentUser, string newUser, int position)
        {
            string sourceFile = @"C:\BoaSorte\Banco\Resultados.txt";

            string[] lines = File.ReadAllLines(sourceFile);

            if (lines.Length == 0)
            {
                MessageBox.Show("Seu arquivo está vazio!");
                return;
            }

            using (StreamWriter writer = new StreamWriter(sourceFile))
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    // Verifica se é a segunda linha e se o conteúdo da mesma é igual ao usuário atual
                    if (i == position && lines[i] == txtConcurso.Text)
                    {
                        writer.WriteLine(ResultTextBox[i].Text);
                    }
                    else
                    {
                        writer.WriteLine(lines[i]);
                    }
                }
            }
        }

I appreciate any advice, opinion or indication
AnswerRe: how to edit and delete lines from a text file PinPopular
OriginalGriff12-Aug-23 20:01
mvaOriginalGriff12-Aug-23 20:01 
GeneralRe: how to edit and delete lines from a text file Pin
Edilson Lemos 202113-Aug-23 12:15
Edilson Lemos 202113-Aug-23 12:15 
GeneralRe: how to edit and delete lines from a text file Pin
OriginalGriff13-Aug-23 20:08
mvaOriginalGriff13-Aug-23 20:08 
AnswerRe: how to edit and delete lines from a text file Pin
Gerry Schmitz13-Aug-23 6:53
mveGerry Schmitz13-Aug-23 6:53 
AnswerRe: how to edit and delete lines from a text file Pin
jschell14-Aug-23 7:07
jschell14-Aug-23 7:07 
Questionthen the new student submits their answer to ... Pin
BillWoodruff8-Aug-23 5:23
professionalBillWoodruff8-Aug-23 5:23 
AnswerRe: then the new student submits their answer to ... Pin
Pete O'Hanlon8-Aug-23 20:33
subeditorPete O'Hanlon8-Aug-23 20:33 
GeneralRe: then the new student submits their answer to ... Pin
BillWoodruff10-Aug-23 1:36
professionalBillWoodruff10-Aug-23 1:36 
GeneralRe: then the new student submits their answer to ... Pin
Pete O'Hanlon10-Aug-23 4:29
subeditorPete O'Hanlon10-Aug-23 4:29 
GeneralRe: then the new student submits their answer to ... Pin
BillWoodruff10-Aug-23 3:09
professionalBillWoodruff10-Aug-23 3:09 
GeneralRe: then the new student submits their answer to ... Pin
Richard MacCutchan10-Aug-23 3:26
mveRichard MacCutchan10-Aug-23 3:26 
GeneralRe: then the new student submits their answer to ... Pin
Richard Deeming10-Aug-23 3:42
mveRichard Deeming10-Aug-23 3:42 
GeneralRe: then the new student submits their answer to ... Pin
BillWoodruff10-Aug-23 13:38
professionalBillWoodruff10-Aug-23 13:38 
GeneralRe: then the new student submits their answer to ... Pin
Richard Deeming10-Aug-23 22:27
mveRichard Deeming10-Aug-23 22:27 
GeneralRe: then the new student submits their answer to ... Pin
Richard Deeming11-Aug-23 0:27
mveRichard Deeming11-Aug-23 0:27 
GeneralRe: then the new student submits their answer to ... Pin
Sandeep Mewara12-Aug-23 18:26
mveSandeep Mewara12-Aug-23 18:26 
QuestionCount DataGridview row values and compare with an Array (Resolved) Pin
Edilson Lemos 20217-Aug-23 15:21
Edilson Lemos 20217-Aug-23 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.