Solved

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

Posted on 2008-06-25
3
3,715 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

15 Experts available now in Live!

Get 1:1 Help Now