IRONSOFTWAREHOME
.NET 帮助

C# 控制台打印:分步指南

Jacob Mellor,Team Iron 的首席技术官
Jacob Mellor
Updated: 2026年6月28日

向控制台打印是 C# 编程的一个基本方面,它允许开发人员显示信息、与用户交互以及调试应用程序。 在这篇全面的文章中,我们将探讨在 C# 中向控制台打印的各种方法,涵盖基本输出、格式化和高级技巧。

基本控制台输出

在C#中向控制台打印的最简单方法是使用WriteLine方法。 此方法将一行文本后跟一个换行符写入标准输出流。

using System;

class Program
{
    public static void Main()
    {
        // Output a simple greeting message to the console
        Console.WriteLine("Hello World, Welcome to C#!");
    }
}

这里,Console.WriteLine语句将"Hello World, Welcome to C#!"输出到控制台。 Console.WriteLine方法自动添加换行符,导致每个后续输出出现在新行上。

用于内联输出的Console.Write

如果您想打印文本而不换行,可以使用Write方法。 这对于以单行形式进行内联或格式化输出非常有用。

using System;

class Program
{
    public static void Main()
    {
        // Use Console.Write to print text inline without a newline
        Console.Write("Hello, ");
        Console.Write("C#!");
    }
}

在此示例中,"Hello, "和"C#!"打印在同一行,因为Console.WriteLine方法那样添加换行符。

格式化输出

C# 提供了多种格式化选项来控制数据在控制台应用程序中的显示方式。 Console.WriteLine方法支持使用格式说明符的复合格式化。

using System;

class Program
{
    public static void Main()
    {
        string name = "John";
        int age = 25;
        // Using composite formatting to print variable values
        Console.WriteLine("Name: {0}, Age: {1}", name, age);
    }
}

这里,**{0}{1}**是age的值的占位符。 这将生成格式化的输出"姓名:John,年龄:25"。

使用字符串插值

字符串插值是 C# 6 中引入的一种简洁的字符串格式化方法。它允许您将表达式直接嵌入到字符串字面量中。

using System;

class Program
{
    public static void Main()
    {
        string name = "Alice";
        int age = 30;
        // Using string interpolation to format output
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}

**$**符号表示字符串插值, **{}**内的表达式将被求值并插入到字符串中。

控制台输出:姓名:Alice,年龄:30

显示当前日期

在这里,我们将探索如何使用Console.WriteLine方法将当前日期打印到控制台。 这是调试、记录日志或向用户提供实时反馈的常见做法。

using System;

class Program
{
    public static void Main()
    {
        // Obtain the current date and time
        DateTime currentDate = DateTime.Now;

        // Print the current date to the console
        Console.WriteLine($"Current Date: {currentDate}");
    }
}

在此示例中,我们利用Console.WriteLine语句将此信息打印到控制台。 最终呈现出清晰简洁的当前日期显示。

控制台输入

除了输出之外,控制台通常还用于用户输入。 Console.ReadLine方法允许您读取用户输入的一行文本。

using System;

class Program
{
    public static void Main(string[] args)
    {
        // Prompt the user to enter their name
        Console.Write("Enter your name: ");
        
        // Read input from the user
        string variable = Console.ReadLine();
        
        // Print a personalized greeting
        Console.WriteLine($"Hello, {variable}!");
    }
}

在此,程序提示用户输入他们的姓名,使用Console.ReadLine读取输入,然后在单个字符串行上打印个性化问候消息。

控制台颜色

您可以更改控制台文本的前景色和背景色,以增强视觉效果。 为此目的使用Console.BackgroundColor属性。

using System;

class Program 
{
    public static void Main()
    { 
        // Set the console text color to green
        Console.ForegroundColor = ConsoleColor.Green;
        
        // Set the console background color to dark blue
        Console.BackgroundColor = ConsoleColor.DarkBlue;
        
        // Print a message with the set colors
        Console.WriteLine("Colored Console Output");
        
        // Reset colors to default
        Console.ResetColor();
    }
}

此代码示例将前景色设置为绿色,背景色设置为深蓝色,然后在文本打印完成后将颜色重置为默认值。

高级控制台输出

对于更高级的场景,比如打印格式化数据、表格或进度指示器,您可以通过NuGet包管理器探索ConsoleTables等第三方库,或使用高级格式化技术实现自定义解决方案。

using System;
using ConsoleTables;

class Program
{
    public static void Main()
    { 
        // Create a new table with specified column headers
        var table = new ConsoleTable("ID", "Name", "Age");
        
        // Add rows to the table
        table.AddRow(1, "John", 25);
        table.AddRow(2, "Alice", 30);
        
        // Print the table to the console
        Console.WriteLine(table);
    }
}

在此示例中,使用ConsoleTables库向控制台打印一个格式化表格。 这使得控制台窗口的输出看起来更加现代、简洁。

Console Window: Printed a formatted table to the console using "ConsoleTables" library.

IronPrint:简化 .NET 打印功能

IronPrint是由 Iron Software 开发的多功能打印库,旨在帮助 .NET 开发人员将强大的打印功能无缝集成到他们的应用程序中。 无论您是开发 Web 应用程序、桌面应用程序还是移动解决方案,IronPrint 都能确保在各种 .NET 平台上提供无缝且可部署的打印体验。

IronPrint for .NET:C#打印库

IronPrint 的主要特点

1. 跨平台支持

IronPrint 与多种环境兼容,确保您的应用程序可以在不同平台上利用其打印功能,包括:

  • Windows(7+,Server 2016+)
  • macOS(10+)
  • iOS(11+)
  • Android API 21+(v5"棒棒糖")

2. .NET 版本支持

该库支持多种 .NET 版本,使其能够灵活应用于各种项目:

  • 支持 .NET 8、7、6、5 和 Core 3.1+
  • .NET Framework (4.6.2+)

3. 项目类型支持

IronPrint 可满足 .NET 生态系统中不同类型的项目需求:

  • 移动端(Xamarin、MAUI 和 Avalonia)
  • 桌面(WPF、MAUI 和 Windows Avalonia)
  • 控制台(应用和库)

4. 广泛的格式支持

IronPrint 支持多种文档格式,包括 PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP。这种灵活性使其成为处理不同类型文档的开发人员的理想选择。

5. 可自定义的打印设置

利用 IronPrint 的自定义设置,打造专属您的打印体验。 调整 DPI,指定份数,设置纸张方向(纵向或横向)等等。 该库使开发人员能够微调打印配置,以满足其应用程序的需求。

要将SharpZipLib集成到你的.NET项目中:

IronPrint 的入门过程非常简单。 请按照以下步骤安装库:

  1. 使用 NuGet 包管理器安装 IronPrint 包:

    # Install IronPrint via NuGet Package Manager
    Install-Package IronPrint
    SHELL

    或者,直接从IronPrint NuGet 官方网站或 NuGet 解决方案程序包管理器下载该程序包。

    Install IronPrint using NuGet Package Manager by searching "IronPrint" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  2. 安装完成后,在 C# 代码顶部添加以下语句即可开始使用 IronPrint:

    using IronPrint;
  3. 通过将许可证密钥值分配给LicenseKey属性来应用有效购买的许可证或试用密钥:

    License.LicenseKey = "IRONPRINT.MYLICENSE.KEY.1EF01";

代码示例

1. 打印文档

using IronPrint 可以简化文档打印过程。 只需将文件路径传递给Print方法:

using IronPrint;

// Print the document
Printer.Print("newDoc.pdf");

2. 带对话框的打印

要在打印之前显示打印对话框,请使用ShowPrintDialog方法:

using IronPrint;

// Show print dialog
Printer.ShowPrintDialog("newDoc.pdf");

3. 自定义打印设置

通过实例化PrintSettings类以编程方式配置打印设置,使用以下代码:

using IronPrint;

// Configure print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;

// Print the document with custom settings
Printer.Print("newDoc.pdf", printSettings);

许可与支持

IronPrint 虽然是付费库,但提供免费试用许可证。 使用 IronPrint 的许可页面申请永久许可。 如需更多支持和咨询,请联系Iron Software 团队

结论

对于 C# 开发人员来说,向控制台输出信息是一项基本技能。 无论是基本的文本输出、格式化字符串、用户输入,还是高级控制台操作,了解各种可用技术都能帮助您创建强大且用户友好的控制台应用程序。 尝试这些方法,并根据您具体项目的需求进行调整。

IronPrint是一款功能强大的 .NET 打印库,以其准确性、易用性和速度而著称。 有关 IronPrint 的更多信息,并探索 IronPrint 提供的全部功能和代码示例,请访问官方文档API 参考页面。

IronPrint 还提供免费试用,以便在商业环境中测试其全部潜力。 但是,试用期结束后,您需要购买许可证。 其Lite套餐起价从$999开始。 从这里下载库文件并试用一下!

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。

相关文章

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户