Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Text is "OK" In file can be only one or up to 500 lines and it mast run on netframework 3.0
I try this: File.AppendAllText(label1.Text, "OK".ToString());
label1 is Path to a file, but this only add text "OK" on the end of text.

154256222 020100LB100EW1555 BEOGRAD TEST1
152256221 020100EW152256221 BEOGRAD TEST1
185256123 020100VV955555552 BEOGRAD TEST1
Posted

There is no function that does what you are looking for (AppendAllText appends to the end of the file as you already saw)...
So you have to do something by yourself...
1. Read-in all the lines using ReadAllLines method[^]
2. Run on the string array you got and add the 'OK' string to each
3. Write-out all the lines using WriteAllLines method[^]
 
Share this answer
 
try as below (.net 3.5 or later)
C#
File.WriteAllLines(filePath, File.ReadAllLines(filePath).Select(line=>line+"OK"));

.net 3.0
C#
string textToAppend = "ok";
List<string> lines = new List<string>();
foreach (var line in File.ReadAllLines(filePath))
{
    lines.Add(line + textToAppend);
}

File.WriteAllLines(filePath, lines.ToArray());
 
Share this answer
 
v2
Comments
Member 11383935 2-Aug-15 5:17am    
'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?
DamithSL 2-Aug-15 5:29am    
you need to have
using System.Linq;
in top of your code file
Member 11383935 2-Aug-15 6:38am    
System.Linq will not work on net framework 3.0
DamithSL 2-Aug-15 6:54am    
check my updated answer
Member 11383935 2-Aug-15 7:18am    
thanks this work perfectly
this is entire code, i want to move file (in button btnSave) and if file exist to overwrite that file, how to do that?
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;



namespace ToEditTxt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            // sakrivanje dugmeta -obradi- i dugmeta -snimi- i promena boje
            btnEdit.Enabled = false;
            btnEdit.BackColor = Color.White;
            btnSave.Enabled = false;
            btnSave.BackColor = Color.White;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            //menjanje boje dugmeta i sakrivanje dugmeta
            btnLoad.Enabled = false;
            btnLoad.BackColor = Color.White;

            // omogućavanje dugmeta  -obradi- i promena boje
            btnEdit.Enabled = true;
            btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
            



            

            //učitavanje fajla
            openFileDialog1.ShowDialog();
            label1.Text = openFileDialog1.FileName;
            label2.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);
            label3.Text = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);

            label4.Visible = true;

            
        }





        private void btnEdit_Click(object sender, EventArgs e)
        {
            //menjanje boje dugmeta i sakrivanje dugmeta
            btnEdit.Enabled = false;
            btnEdit.BackColor = Color.White;


            //dodavanje 1 na kraju svakog reda u texualnom fajlu
            string textToAppend = "1";
            List<string> lines = new List<string>();
            foreach (var line in File.ReadAllLines(label1.Text))
            {
                lines.Add(line + textToAppend);
            }

            File.WriteAllLines(label1.Text, lines.ToArray());

            // omogućavanje dugmeta  -snimi- i promena boje
            btnSave.Enabled = true;
            btnSave.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");


           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //snimanje i preacivanje fajla u drugi direktorijum
                System.IO.FileInfo f = new System.IO.FileInfo(label1.Text);
                f.MoveTo(@"c:\q\" + label3.Text + ".rpt");



                if (File.Exists(@"c:\q\"))
                {
                    File.Delete(@"c:\q\");
                    File.Move(label3.Text + ".rpt", @"c:\q\");
                }
                File.Move(label3.Text + ".rpt", @"c:\q\");
            }

            catch (IOException)
            {



                //resetovanje labela na početne vrednosti
                label1.ResetText();
                label2.ResetText();
                label3.ResetText();
                this.Refresh();
                this.ResetText();
                this.Update();
                // vraćanje grafičkog okruženja na početne vrednosti
                btnLoad.Enabled = true;
                btnEdit.Enabled = true;
                btnLoad.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");
                btnEdit.BackColor = System.Drawing.ColorTranslator.FromHtml("#d3d3d3");

                label5.Visible = true;

            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
        
    

</string></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900