adding listbox content to certain lines of a text file

Hi Everyone,
I was working on this app for a while tonight and i was wondering could someone help me.
I am making a app where you drag and drop
files into the listbox and the filename shows in the listbox i have got that part out the way now i am on getting the filenames in the listbox to be appended to certain lines of an .ini file (for example like listbox entry 1 will go on line 32 so on and so forth.) here is what i hvae so far any and all help is appreciated button2 is where i want the process to occur Click Here to See picture

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;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

{

}

private void listBox1_DragEnter(object sender, DragEventArgs e)

{

// make sure they're actually dropping files (not text or anything else)

if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)

// allow them to continue

// (without this, the cursor stays a "NO" symbol

e.Effect = DragDropEffects.All;

}

private void listBox1_DragDrop(object sender, DragEventArgs e)

{

// transfer the filenames to a string array

// (yes, everything to the left of the "=" can be put in the

// foreach loop in place of "files", but this is easier to understand.)

string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

// loop through the string array, adding each filename to the ListBox

foreach (string file in files)

{

listBox1.Items.Add(file);

}

}

private void button2_Click(object sender, EventArgs e)

{

}

private void button1_Click(object sender, EventArgs e)

{

}

private void button3_Click(object sender, EventArgs e)

{

// To remove currently selected item:

listBox1.Items.Remove(listBox1.SelectedItem);

}

private void button4_Click(object sender, EventArgs e)

{

listBox1.Items.Clear();

}

}

}



Answer this question

adding listbox content to certain lines of a text file

  • LornaJoy

    Here is a more .Net 2.0 way to do it. And with more error checking.

    public static void InsertLines(

    string filename,

    ICollection<string> lines,

    int linenumber)

    {

    if (filename == null)

    {

    throw new ArgumentNullException("filename");

    }

    if (lines == null)

    {

    throw new ArgumentNullException("lines");

    }

    string[] fileLines = File.ReadAllLines(filename);

    int fileLength = fileLines.Length;

    if (linenumber < 0 || linenumber > fileLength)

    {

    throw new ArgumentOutOfRangeException("linenumber");

    }

    int linesCount = lines.Count;

    Array.Resize<string>(

    ref fileLines,

    fileLength + linesCount);

    Array.Copy(

    fileLines,

    linenumber,

    fileLines,

    linenumber + linesCount,

    fileLength - linenumber);

    lines.CopyTo(fileLines, linenumber);

    File.WriteAllLines(filename, fileLines);

    }



  • Morse

    Hi!

    You need to append (to the end) of some text file new lines or insert them into some specific place inside text file

    If you need append - simplest way is File.AppendAllText()



  • Juan Foegen

     BioSlayer wrote:

    Here is a more .Net 2.0 way to do it. And with more error checking.


    Thank you very much BioSlayer for the reply. Still there is a problem with large files, as in all examples.



  • werg

    If your file is not large (less than few MB), then you can open file, read it's content into memory array, change array and write things back. Like this:

    List<string> lines = new List<string>();

    using (System.IO.StreamReader reader = new System.IO.StreamReader(@"c:\file.txt"))

    {

    string line;

    while ((line = reader.ReadLine()) != null)

    {

    lines.Add(line);

    }

    }

    // do changes in lines list

    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"c:\file.txt"))

    {

    foreach (string line in lines)

    {

    writer.WriteLine(line);

    }

    }



  • Dale Shetler

    I think it's still 99.9999% acceptable choice to load whole file in memory - text files larger than 1 MB is really hard to find (except log files, but they typically not need to have insertions, just appending).

    But anyway, PJ. - thanks for interest in performance subject. Did you see this proposal You welcome to drop a line there!

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=329219&SiteID=1



  • XgenX

    i wanted to insert them into a specific place inside the text file

    thanks for the help


  • gtmulg

    Sergey Galich wrote:
    But anyway, PJ. - thanks for interest in performance subject. Did you see this proposal You welcome to drop a line there!

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=329219&SiteID=1



    I dropped a line, sorry for voting the dark-side


  • DavidWalker

    is there a code that i can use that allows me to specify the line to add the new data like add this to line45
  • atv4fun

    If you have all text file inside list of strings, then you can insert into list before saving changes. Check List<T>.Insert() method.

  • C Howell

    Atlantaazfinest wrote:
    is there a code that i can use that allows me to specify the line to add the new data like add this to line45


    Here is a little example:


    public void InsertLines( string filename, string[] lines, int linePosition )
    {
    ArrayList buffer = new ArrayList();

    using(StreamReader reader = new StreamReader( filename ))
    {
    int lineNumber = 0;
    string line = null;
    bool added = false;

    while( (line = reader.ReadLine()) != null )
    {
    lineNumber++;

    if( lineNumber == linePosition )
    {
    buffer.AddRange( lines );
    added = true;
    }
    else
    {
    buffer.Add( line );
    }
    }

    if( !added )
    {
    buffer.AddRange( lines );
    }
    }

    using(StreamWriter writer = new StreamWriter( filename, false ))
    {
    foreach( string line in buffer )
    {
    writer.WriteLine( line );
    }
    }
    }




  • cnSoftware

    Not a problem, more important that somebody vote at last!  Even if it's not what I want. Thanks.

  • adding listbox content to certain lines of a text file