Click here to Skip to main content
14,243,856 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi can some one tell me how to Write to a Text file with Double quotes?

Actually i want to text-file with the Following format

<<MAWB>>,"160-3636 4834",2012/06/12,"CX500",2012/06/08,17,0

But it is bit of hard to write with (") mark:

what i get is :
<<MAWB>>,160-3636 4834,2012/06/12,CX500,2012/06/08,17,0

Not what i expected.hence can some one could tell me a method for write the text file with a (")mark ,that would be a great help for me.Thanks in Advance!!!i mean how to perse the String with double quotes

<<MAWB>>,160-3636 4834,2012/06/12,CX500,2012/06/08,17,0  TO <br />
<br />
<<MAWB>>,"160-3636 4834",2012/06/12,"CX500",2012/06/08,17,0
Updated 22-Jul-17 4:34am
VJ Reddy 8-Jun-12 4:23am
Thank you for accepting the solution :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

To write Double quotes I put \" in string.Example:

textBox1.Text = "He sad:\"When life gives you lemon, you make a lemon party\"";
Manas Bhardwaj 8-Jun-12 2:46am
correct +5!
Rajesh Anuhya 8-Jun-12 3:18am
Good Answer, +5
VJ Reddy 8-Jun-12 4:26am
Good answer. 5!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Like this:

string textToWrite = "<<MAWB>>,\"160-3636 4834\",2012/06/12,\"CX500\",2012/06/03,17,0";

File.WriteAllText(@"C:\Temp\temp.txt", textToWrite);

Look at MSDN documentation [^]for details.
VJ Reddy 8-Jun-12 4:26am
Good answer. 5!
Manas Bhardwaj 8-Jun-12 5:04am
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try to include escape charater [i.e.,back slash] in the code where you are trying to add double quotes.

For ex: "\" YOUR DATA \""

Look for below pointers:[^][^]
VJ Reddy 8-Jun-12 4:27am
Good answer. 5!
Rate this:
Please Sign up or sign in to vote.

Solution 5

The Solutions already given with a suggestion to use \" are good.

As an alternative if you want to use verbatim string literal explained here[^] then "" can be used to include " within a string as shown below:
string text = @"<<MAWB>>,""160-3636 4834"",2012/06/12,""CX500"",2012/06/03,17,0";

//Content of outFile
//<<MAWB>>,"160-3636 4834",2012/06/12,"CX500",2012/06/03,17,0
Hesha 8-Jun-12 4:24am
Thanks Lot VJ Reddy , HIMACHALI , Vani Kulkarni,Manas Bhardwaj For your Support!!! Again Thanks a Lot!!!

vangapally Naveen Kumar 8-Jun-12 4:42am
Already answer is given then why you repost the same answer ?
VJ Reddy 8-Jun-12 4:47am
The answer is not same. In the earlier answers. \" was suggested for ", which I already stated in my solution.

Whereas in my solution I have given "" for " by using verbatim string literal like @"...".
It is easy to use verbatim string instead of remembering which characters are to be escaped (eg. \\, \' etc.) as given here, in the normal string as these characters can be given normally in verbatim string literal except ". For " in verbatim string literal "" can be used.

Hope I clarified my position.
Thank you :)
vangapally Naveen Kumar 8-Jun-12 4:57am
Ok ..........
Manas Bhardwaj 8-Jun-12 5:20am
correct +5
VJ Reddy 8-Jun-12 5:57am
Thank you, Manas :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100