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
my Save button and your Save TXT method
my text file is being saved like this
How I tried the edit method
I appreciate any advice, opinion or indication
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
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
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)
{
}
I appreciate any advice, opinion or indication