.NET 程序下銳浪報表 (Grid++ Report) 的綠色發布指南
在銳浪報表官方為 CSharp 編寫的開發文檔:“在C#與VB.NET中開始使用說明.txt” 中,關於發布項目是這么描述的:
★發布你的項目,用VS.NET制作安裝程序:
C:\Grid++Report 6\Samples\CSharp\在C#與VB.NET中開始使用說明.txt
1、先創建安裝項目:在解決方案資源管理器的根節點上點右鍵,在彈出的菜單里執行“添加->新建項目…”,創建一個“安裝項目”。
2、加入項目發布文件:在解決方案資源管理器的新創建的安裝項目節點上點右鍵,在彈出的菜單里執行“添加->項目輸出…”,在打開的窗口里選擇“主輸出”,這樣可以把程序的發布文件自動加入到安裝項目中。
3、設置Grid++Report的DLL文件為COM自注冊:分別設置“gregn6.dll”與“grdes6.dll”的“Register”屬性為“vsdrfCOMSelfReg”。
這種方式需要我們在運行程序之前將 Grid++Report 依賴的 COM 組件注冊到操作系統中。有沒有辦法可以做到綠色發布呢?
以 C:\Grid++Report 6\Samples\CSharp\1.Tutorial\PrintReport\PrintReport.sln 項目為例,我們對項目進行改造,使其支持綠色發布( xcopy-deploy):
- 在項目中增加
Files文件夾 - 將項目依賴的數據文件
1a.簡單表格.grf和Northwind.mdb復制到 Files 文件夾,並設置文件屬性 “復制到輸出目錄” 為 “始終復制”。 - 將
C:\Grid++Report 6\目錄下的 “gregn6.dll”與“grdes6.dll” 復制到項目中,並設置文件屬性 “復制到輸出目錄” 為 “始終復制”。
做完以上三步,解決方案管理器看上去應該是這個樣子:(筆者對源文件進行了一些改動,並將 Form1 重命名為了 FrmMain 。)

接着,對代碼進行改造,使其從程序所在目錄讀取數據,而不是 Grid++Report 的安裝目錄:
public FrmMain() { // // Windows 窗體設計器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼 // //載入報表模板文件,必須保證 Grid++Report 的安裝目錄在‘C:\Grid++Report 6’下, //關於動態設置報表路徑與數據綁定參數請參考其它例子程序 var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files"); Report.LoadFromFile(Path.Combine(dir, "1a.簡單表格.grf")); Report.DetailGrid.Recordset.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"User ID=Admin;Data Source=" + Path.Combine(dir, "Northwind.mdb"); }
如果你在電腦中安裝了最新版的 Grid++Report ,那么你會有以下目錄:C:\Grid++Report 6\manifest
本目錄下的兩個 .manifest 文件為Grid++Report的發布DLL的清單模板文件,利用這兩個文件並適當調整可以實現免DLL注冊發布用Grid++Report開發的軟件,實現綠色發布。
C:\Grid++Report 6\manifest\readme.txt
更詳細說明請參考幫助文檔的“報表組件(C/S報表)->發布軟件->免注冊發布(推薦方式)”部分。
YourX86.exe.manifest:32位程序清單文件模板
YourX64.exe.manifest:64位程序清單文件模板
在項目中添加一個新建項 “應用程序清單文件”:

之后需要打開 YourX86.exe.manifest 文件,並將兩個 file 標簽的內容復制到項目的 app.manifest 文件中。這個操作完成后,app.manifest 的代碼如下:
<?xml version="1.0" encoding="utf-8"?> <assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <!-- UAC 清單選項 如果想要更改 Windows 用戶帳戶控制級別,請使用 以下節點之一替換 requestedExecutionLevel 節點。n <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 指定 requestedExecutionLevel 元素將禁用文件和注冊表虛擬化。 如果你的應用程序需要此虛擬化來實現向后兼容性,則刪除此 元素。 --> <requestedExecutionLevel level="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- 設計此應用程序與其一起工作且已針對此應用程序進行測試的 Windows 版本的列表。取消評論適當的元素, Windows 將自動選擇最兼容的環境。 --> <!-- Windows Vista --> <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />--> <!-- Windows 7 --> <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />--> <!-- Windows 8 --> <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />--> <!-- Windows 8.1 --> <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />--> <!-- Windows 10 --> <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />--> </application> </compatibility> <!-- 指示該應用程序可以感知 DPI 且 Windows 在 DPI 較高時將不會對其進行 自動縮放。Windows Presentation Foundation (WPF)應用程序自動感知 DPI,無需 選擇加入。選擇加入此設置的 Windows 窗體應用程序(目標設定為 .NET Framework 4.6 )還應 在其 app.config 中將 "EnableWindowsFormsHighDpiAutoResizing" 設置設置為 "true"。--> <!-- <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> </windowsSettings> </application> --> <!-- 啟用 Windows 公共控件和對話框的主題(Windows XP 和更高版本) --> <!-- <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> --> <file name="grdes6.dll"> <typelib tlbid="{c5a16330-a084-48c9-bc0f-0d0b37a14123}" version="6.0" helpdir="" resourceid="0" flags="HASDISKIMAGE" /> <comClass clsid="{6edd80cb-9f08-4c71-b406-479e5cb80fce}" threadingModel="Apartment" tlbid="{c5a16330-a084-48c9-bc0f-0d0b37a14123}" progid="grdes.GRDesigner.6" description="Grid++Report Designer 6" /> </file> <file name="gregn6.dll"> <typelib tlbid="{4018f953-1bfe-441e-8a04-dc8ba1ff060e}" version="6.0" helpdir="" resourceid="0" flags="HASDISKIMAGE" /> <comClass clsid="{f9364159-6aed-4f9c-8baf-d7c7ed6160a8}" threadingModel="Apartment" tlbid="{4018f953-1bfe-441e-8a04-dc8ba1ff060e}" progid="gregn.GridppReport.6" description="GridppReport Class" /> <comClass clsid="{1b5ea181-a38d-4f42-88b2-6af74cf6d6c0}" threadingModel="Apartment" tlbid="{4018f953-1bfe-441e-8a04-dc8ba1ff060e}" progid="gregn.GRDisplayViewer.6" description="Grid++Report DisplayViewer 6" /> <comClass