简体   繁体   中英

Writing to a text File in C#

Hi i am trying to write to a text file in C# console but the text file needs to include " i can not find anything that includes these when writing to a text file. The error i get is too many characters in character literal.

I have tried the following as well

StreamWriter File = new StreamWriter("Test.csproj");

File.Write('<Project ToolsVersion= "14.0"DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003>"');

I can not figure out what i am doing wrong.

this what needs to be written in the text file

<Project ToolsVersion= "14.0"DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003>"

Strings in C# must be surrounded with double quotes, not single quotes, and you can escape them with a back-slash (\) character.

File.Write("<Project ToolsVersion=\"14.0\" ... ");

Single quotes in C# are only for single characters , which is why the error complains about too many characters in a character literal.

This is also missing whitespace between "14.0" and DefaultTargets , and misplaces the final angle bracket. The exact string you want to write almost certainly looks like this:

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

But it seems strange to me that someone new to the language is already trying to build MS Build files programmatically, and there are other tools in the platform for dealing with Xml. What are you trying to accomplish?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM