为什么带有 System.Console.WriteLine 的单行应用程序会给我一个语法错误?

发布于 2024-12-27 04:48:36 字数 404 浏览 6 评论 0原文

在多次令人沮丧的经历之后,我决定再次尝试使用 Windows 开发人员预览版构建 Windows Metro 风格的应用程序。

所以我启动了 Visual Studio,然后砰的一声!当我尝试输入此代码时,

System.Console.WriteLine("");

它在“控制台”下显示红色波浪线。

所以我尝试了其他方法:

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

但也失败了!文件下呈红色波浪状。

为什么会弹出这个错误?所有这些代码应该可以正常工作!或者我犯了一个基本错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为 Metro 应用程序使用 .Net Framework API 的子集。在此版本中 System.ConsoleSystem.IO.File 不存在。

来自 MSDN,“将 System.IO.File.ReadAllText 方法替换为使用异步 I/O 成员的方法;例如:”

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());
    }
}

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());
    }
}
风尘浪孓 2025-01-03 04:48:36

Windows 8 Metro 应用程序没有控制台(因为图形环境中的控制台没有意义),但您可以通过第三方库(例如 Metro 应用程序的控制台类

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文