HOW TO:發行具有指定地區的專案
在應用程式中包含不同地區設定 (Locale) 的元件並不常見。 在這個情形下,您可能會建立一個內含數個專案的方案,然後針對每個地區設定發行不同的專案。 本程序會示範如何使用巨集,利用 'en' 地區設定發行方案內的第一個專案。 如果想要使用 'en' 以外的地區設定來嘗試執行這個程序,請確定已在巨集內將 localeString 設定為符合您所使用的地區設定 (例如,'de' 或 'de-DE')。
![]() |
---|
使用這個巨集時,[發行位置] 應為有效的 URL 或通用命名慣例 (Universal Naming Convention,UNC) 共用。 此外,電腦中也必須安裝網際網路資訊服務 (IIS)。 若要安裝 IIS,請在 [開始] 功能表上,按一下 [控制台], 然後按兩下 [新增或移除程式]。 按一下 [新增或移除程式] 中的 [新增/移除 Windows 元件]。 在 [Windows 元件精靈] 的 [元件] 清單中,選取 [網際網路資訊服務 (IIS)] 核取方塊, 然後按一下 [完成] 關閉精靈。 |
若要建立發行巨集
若要開啟 [巨集總管],請指向 [工具] 功能表上的 [巨集],然後按一下 [巨集總管]。
建立新的巨集模組。 在 [巨集總管] 中,選取 [MyMacros]。 指向 [工具] 功能表上的 [巨集],然後按一下 [新增巨集模組]。 將此模組命名為 PublishSpecificCulture。
在 [巨集總管] 中,展開 [MyMacros] 節點,然後按兩下 [PublishAllProjects] 模組將它開啟 (或是指向 [工具] 功能表上的 [巨集],然後按一下 [巨集 IDE])。
在 [巨集 IDE] 中,將下列程式碼加入該模組的 Import 陳述式之後:
Module PublishSpecificCulture Sub PublishProjectFirstProjectWithEnLocale() ' Note: You should publish projects by using the IDE at least once ' before you use this macro. Items such as the certficate and the ' security zone must be set. Dim localeString As String = "en" ' Get first project. Dim proj As Project = DTE.Solution.Projects.Item(1) Dim publishProperties As Object = proj.Properties.Item("Publish").Value ' GenerateManifests and SignManifests must always be set to ' True for publishing to work. proj.Properties.Item("GenerateManifests").Value = True proj.Properties.Item("SignManifests").Value = True 'Set the publish language. 'This will set the deployment language and pick up all ' en resource dlls: Dim originalTargetCulture As String = _ publishProperties.Item("TargetCulture").Value publishProperties.Item("TargetCulture").Value = localeString 'Append 'en' to end of publish, install, and update URLs if needed: Dim originalPublishUrl As String = _ publishProperties.Item("PublishUrl").Value Dim originalInstallUrl As String = _ publishProperties.Item("InstallUrl").Value Dim originalUpdateUrl As String = _ publishProperties.Item("UpdateUrl").Value publishProperties.Item("PublishUrl").Value = _ AppendStringToUrl(localeString, New Uri(originalPublishUrl)) If originalInstallUrl <> String.Empty Then publishProperties.Item("InstallUrl").Value = _ AppendStringToUrl(localeString, New Uri(originalInstallUrl)) End If If originalUpdateUrl <> String.Empty Then publishProperties.Item("UpdateUrl").Value = _ AppendStringToUrl(localeString, New Uri(originalUpdateUrl)) End If proj.Save() Dim slnbld2 As SolutionBuild2 = _ CType(DTE.Solution.SolutionBuild, SolutionBuild2) slnbld2.Clean(True) slnbld2.BuildProject( _ proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, _ proj.UniqueName, True) ' Only publish if build is successful. If slnbld2.LastBuildInfo <> 0 Then MsgBox("Build failed for " & proj.UniqueName) Else slnbld2.PublishProject( _ proj.ConfigurationManager.ActiveConfiguration.ConfigurationName, _ proj.UniqueName, True) If slnbld2.LastPublishInfo = 0 Then MsgBox("Publish succeeded for " & proj.UniqueName _ & vbCrLf & "." _ & " Publish Language was '" & localeString & "'.") Else MsgBox("Publish failed for " & proj.UniqueName) End If End If ' Return URLs and target culture to their previous state. publishProperties.Item("PublishUrl").Value = originalPublishUrl publishProperties.Item("InstallUrl").Value = originalInstallUrl publishProperties.Item("UpdateUrl").Value = originalUpdateUrl publishProperties.Item("TargetCulture").Value = originalTargetCulture proj.Save() End Sub Private Function AppendStringToUrl(ByVal str As String, _ ByVal baseUri As Uri) As String Dim returnValue As String = baseUri.OriginalString If baseUri.IsFile OrElse baseUri.IsUnc Then returnValue = IO.Path.Combine(baseUri.OriginalString, str) Else If Not baseUri.ToString.EndsWith("/") Then returnValue = baseUri.OriginalString & "/" & str Else returnValue = baseUri.OriginalString & str End If End If Return returnValue End Function End Module
關閉 [巨集 IDE]。 焦點將回到 Visual Studio。
若要針對特定地區設定發行專案
若要建立 Visual Basic Windows 應用程式專案,請指向 [檔案] 功能表上的 [新增],然後按一下 [專案]。
在 [新增專案] 對話方塊中,從 [Visual Basic] 節點選取 [Windows 應用程式], 並將專案命名為 PublishLocales。
按一下 [Form1]。 在 [屬性] 視窗的 [設計] 下,將 [Language] 屬性從 [(預設)] 變更為 [英文]。 將表單的 [Text] 屬性變更為 MyForm。
請注意,當地語系化資源 DLL 只有在需要時才會建立。 例如,在您指定新的地區設定後,要變更表單的文字或其中一個控制項時就會建立。
使用 Visual Studio IDE 發行 PublishLocales。
在 [方案總管] 中,選取 [PublishLocales]。 選取 [專案] 功能表上的 [屬性]。 在 [專案設計工具] 的 [發行] 頁面,指定發行位置為 https://localhost/PublishLocales,然後按一下 [立即發行]。
當發行網頁出現時,將其關閉 (在這個步驟中,您只需要發行專案,而不需要安裝專案)。
在 Visual Studio 的 [命令提示字元] 視窗中叫用此巨集,再次發行 PublishLocales。 若要檢視 [命令提示字元] 視窗,請指向 [檢視] 功能表上的 [其他視窗],然後按一下 [命令視窗] 或按 CTRL+ALT+A。 在 [命令提示字元] 視窗中輸入 macros,自動完成便會提供可用的巨集清單。 選取下列巨集,然後按 ENTER 鍵:
Macros.MyMacros.PublishSpecificCulture.PublishProjectFirstProjectWithEnLocale
當發行程序成功時,將會產生一個訊息,指出:「PublishLocales\PublishLocales.vbproj 發行成功。 發行語言為 'en'」。按一下訊息方塊中的 [確定]。 當發行網頁出現時,按一下 [安裝]。
查看 C:\Inetpub\wwwroot\PublishLocales\en, 除了當地語系化資源 DLL 以外,您還應該查看安裝的檔案,例如資訊清單、setup.exe 和發行網頁檔 (根據預設,ClickOnce 會在 EXE 和 DLL 中附加 .deploy 副檔名,您可以在部署之後移除此副檔名)。