全局程序集GlobalAssemblyInfo.cs及WinForm程序如何在Debug模式下启动控制台窗口Console

全局程序集GlobalAssemblyInfo.cs
我们编写的一个解决方案,通常会包含多个项目,而每个项目都有单独的程序集信息AssemblyInfo.cs。但是,你会发现一个问题,这些AssemblyInfo.cs当中有一部分在重复的,若能把它们提取出来放入一个单一文件中,修改AssemblyInfo中的诸如产品名、产品版本、版本等信息会变得轻松。那么,这个程序集信息文件,我们叫做它——GlobalAssemblyInfo.cs

制作步骤:

在解决方案中添加名为GlobalAssemblyInfo.cs的C#类文件
清除GlobalAssemblyInfo.cs中的代码,然后将如下代码粘贴进去
view plaincopy to clipboardprint?
/* ============================== 
 * 全局程序集信息 
 * GlobalAssemblyInfo.cs 
 *  
 * 请把此文件引用到其他的项目中 
 ==============================*/ 
 
using System.Reflection;  
using System.Runtime.InteropServices;  
 
[assembly: ComVisible(false)]  
[assembly: AssemblyProduct("全局程序集和能输出调试信息到控制台的WinForm项目的演示")]  
[assembly: AssemblyCompany("Oyi319的公司")]  
[assembly: AssemblyVersion(RevisionClass.FullVersion)] 
#if DEBUG   
[assembly : AssemblyConfiguration("Debug")]  
#else  
[assembly: AssemblyConfiguration("Release")] 
#endif  
[assembly: AssemblyCopyright("版权所有 2010 Oyi319")]  
[assembly: AssemblyTrademark("")]  
[assembly: AssemblyCulture("")]  
 
 
internal static class RevisionClass  
{  
    public const string Major = "1";  
    public const string Minor = "0";  
    public const string Build = "0";  
    public const string Revision = "0";  
 
    public const string MainVersion = Major + "." + Minor;  
    public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision;  
}  
 
 
 
/* 
 
其他程序集的AssemblyInfo.cs简化如下内容 
所有信息数据单独填写 
 
using System; 
using System.Reflection; 
using System.Runtime.CompilerServices; 
using System.Runtime.InteropServices; 
 
[assembly: AssemblyTitle("程序集标题")] 
[assembly: AssemblyDescription("程序集描述")] 
 
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 
[assembly: Guid("6a6263f2-35d2-4077-a1aa-cc775ca7cf84")] 
 
[assembly: CLSCompliant(true)] 
[assembly: StringFreezing()] 
*/ 
/* ==============================
 * 全局程序集信息
 * GlobalAssemblyInfo.cs
 *
 * 请把此文件引用到其他的项目中
 ==============================*/

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
[assembly: AssemblyProduct("全局程序集和能输出调试信息到控制台的WinForm项目的演示")]
[assembly: AssemblyCompany("Oyi319的公司")]
[assembly: AssemblyVersion(RevisionClass.FullVersion)]
#if DEBUG
[assembly : AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
[assembly: AssemblyCopyright("版权所有 2010 Oyi319")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]


internal static class RevisionClass
{
    public const string Major = "1";
    public const string Minor = "0";
    public const string Build = "0";
    public const string Revision = "0";

    public const string MainVersion = Major + "." + Minor;
    public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision;
}

 

/*

其他程序集的AssemblyInfo.cs简化如下内容
所有信息数据单独填写

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("程序集标题")]
[assembly: AssemblyDescription("程序集描述")]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("6a6263f2-35d2-4077-a1aa-cc775ca7cf84")]

[assembly: CLSCompliant(true)]
[assembly: StringFreezing()]
*/
 
为每个项目引用GlobalAssemblyInfo.cs文件。
右键项目->添加->现有项,浏览到GlobalAssemblyInfo.cs文件,注意点击“添加”下拉菜单的“添加为链接”将它添加到项目,
然后将这个链接到GlobalAssemblyInfo.cs的文件移到到Properties文件夹下,使它与AssemblyInfo.cs处于同一个文件夹
修改AssemblyInfo.cs文件,将GlobalAssemblyInfo.cs中已经包含的信息去除,例如:
view plaincopy to clipboardprint?
using System;  
using System.Reflection;  
using System.Runtime.CompilerServices;  
using System.Runtime.InteropServices;  
 
[assembly: AssemblyTitle("程序集标题")]  
[assembly: AssemblyDescription("程序集描述")]  
 
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID  
[assembly: Guid("6a6263f2-35d2-4077-a1aa-cc775ca7cf84")]  
 
[assembly: CLSCompliant(true)]  
[assembly: StringFreezing()] 
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("程序集标题")]
[assembly: AssemblyDescription("程序集描述")]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("6a6263f2-35d2-4077-a1aa-cc775ca7cf84")]

[assembly: CLSCompliant(true)]
[assembly: StringFreezing()]
将其他需要全局程序集的项目做同样处理,最后的“解决方案资源管理器”窗口的截图类似这样:
 
2.WinForm程序和控制台窗口Console
如果你调试过SharpDevelop的源程序,会发现它在DEBUG模式时会出现一个控制台窗口,以显示日志信息。或许我使用的方法与其不同,不过你可以试一试,写出我们自己的调试日志代码。

首先要解决的问题是如何在Debug模式时显示Console窗口。我确定,这是一个WinForm项目,也没有改过它的输出类型。我们需要在项目的入口点使用一些API函数将控制台显示出来:

它们是 AllocConsole 和 FreeConsole。

view plaincopy to clipboardprint?
[DllImport("kernel32.dll")]  
public static extern Boolean AllocConsole();  
[DllImport("kernel32.dll")]  
public static extern Boolean FreeConsole(); 
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();

然后我们使它在Main()开始处判断DEBUG编译标记,调用AllocConsole方法显示控制台,然后在Main()的结束处判断DEBUG编译标记,调用FreeConsole方法关闭控制台。这样,我们就可以使用Console.Write等方法将调试信息显示在这个控制台窗口里。

为了达到更好的效果,我们写一个Shell类,用它来封装Console.WriteLine方法,输出个性化信息。我是这样做的,根据输出到控制台的文本的前几个字判断为“警告”、“错误”、“注意”时,输出带有黄色、红色、绿色的文字,其他输出信息输出控制台缺省的灰色文字,以起到区分效果,还要在每条信息前加上输出信息的当时时间。

这个Shell类是这样的:

view plaincopy to clipboardprint?
/// <summary>  
/// 与控制台交互  
/// </summary>  
static class Shell  
{  
    /// <summary>  
    /// 输出信息  
    /// </summary>  
    /// <param name="format"></param>  
    /// <param name="args"></param>  
    public static void WriteLine(string format, params object[] args)  
    {  
        WriteLine(string.Format(format, args));  
    }  
 
    /// <summary>  
    /// 输出信息  
    /// </summary>  
    /// <param name="output"></param>  
    public static void WriteLine(string output)  
    {  
        Console.ForegroundColor = GetConsoleColor(output);  
        Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output);  
    }  
 
    /// <summary>  
    /// 根据输出文本选择控制台文字颜色  
    /// </summary>  
    /// <param name="output"></param>  
    /// <returns></returns>  
    private static ConsoleColor GetConsoleColor(string output)  
    {  
        if (output.StartsWith("警告")) return ConsoleColor.Yellow;  
        if (output.StartsWith("错误")) return ConsoleColor.Red;  
        if (output.StartsWith("注意")) return ConsoleColor.Green;  
        return ConsoleColor.Gray;  
    }  

/// <summary>
/// 与控制台交互
/// </summary>
static class Shell
{
    /// <summary>
    /// 输出信息
    /// </summary>
    /// <param name="format"></param>
    /// <param name="args"></param>
    public static void WriteLine(string format, params object[] args)
    {
        WriteLine(string.Format(format, args));
    }

    /// <summary>
    /// 输出信息
    /// </summary>
    /// <param name="output"></param>
    public static void WriteLine(string output)
    {
        Console.ForegroundColor = GetConsoleColor(output);
        Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output);
    }

    /// <summary>
    /// 根据输出文本选择控制台文字颜色
    /// </summary>
    /// <param name="output"></param>
    /// <returns></returns>
    private static ConsoleColor GetConsoleColor(string output)
    {
        if (output.StartsWith("警告")) return ConsoleColor.Yellow;
        if (output.StartsWith("错误")) return ConsoleColor.Red;
        if (output.StartsWith("注意")) return ConsoleColor.Green;
        return ConsoleColor.Gray;
    }
}

那么程序入口函数Main代码如下:

view plaincopy to clipboardprint?
/// <summary>  
/// 应用程序的主入口点。  
/// </summary>  
[STAThread]  
static void Main()  

#if DEBUG  
    AllocConsole();  
    Shell.WriteLine("注意:启动程序...");  
 
    Shell.WriteLine("/tWritten by ");  
    Shell.WriteLine("/tBlog: ");  
    Shell.WriteLine("{0}:{1}", "警告", "这是一条警告信息。");  
    Shell.WriteLine("{0}:{1}", "错误", "这是一条错误信息!");  
    Shell.WriteLine("{0}:{1}", "注意", "这是一条需要的注意信息。");  
    Shell.WriteLine(""); 
#endif  
    Application.EnableVisualStyles();  
    Application.SetCompatibleTextRenderingDefault(false);  
    Application.Run(new Form1()); 
#if DEBUG  
    Shell.WriteLine("注意:2秒后关闭...");  
    Thread.Sleep(2000);  
    FreeConsole(); 
#endif  

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
#if DEBUG
    AllocConsole();
    Shell.WriteLine("注意:启动程序...");

    Shell.WriteLine("/tWritten by ");
    Shell.WriteLine("/tBlog: ");
    Shell.WriteLine("{0}:{1}", "警告", "这是一条警告信息。");
    Shell.WriteLine("{0}:{1}", "错误", "这是一条错误信息!");
    Shell.WriteLine("{0}:{1}", "注意", "这是一条需要的注意信息。");
    Shell.WriteLine("");
#endif
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
#if DEBUG
    Shell.WriteLine("注意:2秒后关闭...");
    Thread.Sleep(2000);
    FreeConsole();
#endif
}

现在这个控制台窗口,只会在DEBUG模式时显示,而在Release编译时不会出现。 这是不是你想要的调试方法呢?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值