Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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