开发者

Write to Specific line in text file C#

I have a web app that I am developing at work. I need to be able to tak开发者_开发问答e input data and append a text file after (x) number of lines.

My web app is using asp.net with c#

Can anyone help me please?


There's no way of "inserting" into a file in general - or of going to a specific line, without reading all the others, unless they're of a fixed size (in bytes).

Normally the approach would be something like:

  • Start writing a new file
  • Open the existing file
  • Copy the first x lines from the old file to the new one
  • Write the new line
  • Copy the remaining lines from the old file to the new one
  • Move the old one to a backup file
  • Move the new file to the old name
  • Delete the backup file

(This ensures that at any one point there's at least the old file in some form. You can make it slightly simpler if you just delete the old file and then move the new one into place.)

Don't forget to ensure this is synchronized appropriately - you don't want to have two copies of this algorithm running at the same time...

EDIT: If you've got XML files, then I'd suggest usually just loading it into the DOM (e.g. with LINQ to XML), making the change, and then saving it out again. Don't treat it just like an unstructured text file.

You could potentially make this more efficient using XmlReader and XmlWriter - but you're certainly going to have to read the whole original file and write out the new file. Have you benchmarked simple code and found it too slow? How often are you doing this? How big are the files?


I would suggest finding another strategy, specifically a relational database management system. A text file lives on the file system and does not support concurrent access like a good (read:not Access) database. A web application does support concurrent requests. Once you have more than one user working at the same time, your app will experience IO Exceptions.


OK - Thanks to your help Jon I have figured it out.


            FileInfo fi = new FileInfo(Server.MapPath("~/Playlists/" + user + "/" + ListBox1.SelectedItem.Text  + ".wpl"));
            XmlDocument originalXML = new XmlDocument();
            originalXML.Load(fi.FullName);
            XmlWriter newXML = XmlWriter.Create(Server.MapPath("~/Playlists/" + user + "/" + ListBox1.SelectedItem.Text + ".wpl"));
            XmlNode smil = originalXML.SelectSingleNode("smil/body/seq");
            XmlNode media = originalXML.CreateNode(XmlNodeType.Element, "media", null);
            XmlAttribute src = originalXML.CreateAttribute("src");
            DirectoryInfo di = new DirectoryInfo(Server.MapPath("~" + folder));


            foreach (FileInfo file in di.GetFiles("*", SearchOption.AllDirectories))
            {
                string path = file.FullName;
                path = path.Replace(@"F:\Music\Music by Artist", "http://bgab-mor01-n/Music");
                path = path.Replace(@"\", "/");
                path = path.Replace(",", "");
                path = path.Replace("'", "");
                path = path.Replace("&", "");
                if (file.Extension == ".mp3" || file.Extension == ".wma" || file.Extension == ".MP3")
                {
                    src.Value = path;
                    media.Attributes.Append(src);
                    smil.AppendChild(media);

                }

            }

            originalXML.Save(newXML);
            newXML.Close();

I really couldn't have done it without you. You are the man. Thanks for everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜