Why does a single line app with System.Console.WriteLine give me a syntax error?

≡放荡痞女 提交于 2019-12-31 05:07:05

问题


I decided to have a go at building Windows Metro style apps with the Windows Developer Preview again, after multiple frustrating experiences.

So I fire up Visual Studio and BAM! Right as I try to type in this code

System.Console.WriteLine("");

it gives me the red squiggly under "Console".

So I try something else:

string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

and THAT fails as well! Red squiggly under File.

Why is this error popping up? All this code should work fine! Or am I making a basic mistake?


回答1:


Because Metro applications use a subset of the .Net Framework API. In this version System.Console and System.IO.File do not exist.

From MSDN, "Replace System.IO.File.ReadAllText method with a method that uses the asynchronous I/O members; for example:"

public static async Task<string> ReadAllText(StorageFile file)
{
    IInputStream inputStream = await file.OpenForReadAsync();
    using (Stream stream = inputStream.AsStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        return await Task.Run(() => reader.ReadToEnd());
    }
}



回答2:


Windows 8 Metro Apps don't have a console (as the console in a graphical environment doesn't make sense), but you can get some of the functionality of the console with third party libraries like Console Class for Metro Apps



来源:https://stackoverflow.com/questions/8882628/why-does-a-single-line-app-with-system-console-writeline-give-me-a-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!