Solved

Using C# open text file and add text to the beginning and end  of each line

Posted on 2008-06-25
3
3,712 Views
Last Modified: 2010-04-21
Hi,

I want to open a text file then add an open quotation mark to the beginning of each line and a quotation mark and a plus sign to the end of each line.

I am trying to do this in C# but cannot find any good examples.

Thanks for any help,
Bonnie
0
Comment
Question by:Bonnie_K
3 Comments
 
LVL 6

Expert Comment

by:Sh4d1
ID: 21865030
I suggest to read file in for/while read all lines into array and then with regular expressions add at the start and end of line what you want, then write changes back to file.
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 21865077
There are plently of ways to do this, here is one of them:

        String inputFile = Server.MapPath("./Test.txt");
        StreamReader sr = new StreamReader(inputFile);
        String filecontents = sr.ReadToEnd();
        sr.Close();

        StringReader gr = new StringReader(filecontents);
        StreamWriter gw = new StreamWriter(inputFile, false);

        while (gr.Peek() != -1)
        {
            gw.WriteLine("\"" + gr.ReadLine() + "\"+");
        }

        gr.Close();
        gw.Close();
0
 

Author Closing Comment

by:Bonnie_K
ID: 31470534
Thank you very much, works perfectly!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now