2011年11月7日 星期一

Deploying A C# Application (Visual Studio Setup Project)

In this tutorial I will show how to deploy a C# application using a standard Visual Studio Setup Project.

Any application must be somehow provided to the end-user. If the application is very small and is composed of a single executable, it can be deployed by just providing the executable to the end user. In our situation, the application has many dependencies, that can or cannot be present on the client machine, so we need to create a setup project, that will install the needed dependencies and the application itself.

Special Tutorial Requirements:

  • Microsoft Visual Studio 2008 ('Setup and Deployment' projects are not available in Express editions)

Let's start.

1. First of all we need to open a C# project/solution (or any other project/solution you have made using one of the programming languages included in Visual Studio). Use 'File -> Open -> Project/Solution' (or Ctrl+Shift+O):

Posted Image

Resized to 82% (was 610 x 480) - Click image to enlargePosted Image


2. After opening a C# project/solution you will get something like this:

Resized to 49% (was 1024 x 733) - Click image to enlargePosted Image


3. Now you have to add a setup project to the current solution. Use 'File -> Add -> New Project...':

Resized to 100% (was 502 x 385) - Click image to enlargePosted Image


4. In the 'Project types' list, go to 'Other Projects -> Setup and Deployment' and select the 'Setup Wizard' template:

Resized to 63% (was 804 x 512) - Click image to enlargePosted Image


As you see there are many 'Setup and Deployment' project types and you can create just a blank setup project and customize it by yourself, but in this tutorial I will show how to use the wizard, which is a little bit easier for beginners.

5. Now let's take a deeper look at the wizard.

The wizard is composed of 5 general steps:

Step #1: Initialization

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


This step is a 'welcome message'.

Step #2: Project Type

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


In this window you have to select the setup project type. You have four options:

To create a setup program to install an application:
  • Create a setup for a Windows application
  • Create a setup for a web application


To create a redistributable package:
  • Create a merge module for Windows Installer
  • Create a downloadable CAB file

I will use the 'Create a setup for a Windows application', as my C# project is a Windows application.

Step #3: Project Outputs

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


Here you must select the project outputs you wish to include in your installation package. For my project I select only 'Primary output', which includes the application DLL or EXE.

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


Step #4: Files to include

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


Here you have the opportunity to add additional files, like guides, HTML pages or just a ReadMe file. I will add just a ReadMe file:

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


Step #5: Confirmation

Resized to 76% (was 658 x 505) - Click image to enlargePosted Image


Here you can see the settings for the project that will be created. If you find a mistake or something, you can easily go back and change a specific setting.

6. If the project was successfully created, you will see this:

Resized to 49% (was 1024 x 768) - Click image to enlargePosted Image


7. Now you can customize the setup project properties. To access the project properties grid, click on Setup1 project in the 'Solution Explorer' window:

Here are the properties and a short explanation of what they do:

AddRemoveProgramsIcon - Specifies an icon to be diplayed in the Add/Remove Programs dialog.
Author - Specifies the name of the author of an application or component.
Description - Specifies a free-form description for an installer.
DetectNewerInstalledVersion - Specifies whether to check for newer versions of an application on the user's computer.
InstallAllUsers - Specifies whether the package is installed for all users or only the current user.
Keywords - Specifies keywords used to search for an installer.
Localization - Specifies the locale for string resources and the run-time user interface.
Manufacturer - Specifies the name of the manufacturer of an application or component.
ManufacturerUrl - Specifies a URL for a Web site containing information about the manufacturer of an application or component.
PostBuildEvent - Specifies any commands to execute after the build ends.
PreBuildEvent - Specifies any commands to execute before the build starts.
ProductCode - Specifies a unique identifier for an application.
ProductName - Specifies a public name that describes an application or component.
RemovePreviousVersions - Specifies whether an installer will remove previous versions of an application or component.
RunPostBuildEvent - Specifies the condition under which the post-build event runs.
SearchPath - Specifies the path that is used to search for assemblies, files or merge modules on the development computer.
Subject - Specifies additional information describing an application or component.
SupportPhone - Specifies a phone number for support information for an application or component.
SupportUrl - Specifies a URL for a Web site containing support information for an application or component.
TargetPlatform - Specifies the target platform of the installer.
Title - Specifies the title of an installer.
UpgradeCode - Specifies a shared identifier that represents multiple versions of an application or component.
Version - Specifies the version number of an installer, merge module, or .cab file.

8. You can also specify what folders to create on the desktop and in the Start menu:

Resized to 64% (was 789 x 445) - Click image to enlargePosted Image


9. You can also customize the entries that will be added to the registry:

Posted Image

Resized to 63% (was 795 x 445) - Click image to enlargePosted Image


10. You can also customize the file types on the target machine:

Posted Image

Resized to 64% (was 793 x 445) - Click image to enlargePosted Image


11. You can also customize the user interface of the installer:

Posted Image

Resized to 63% (was 795 x 447) - Click image to enlargePosted Image


You can add additional dialogs:

Posted Image

12. You can also customize the custom actions:

Posted Image

Resized to 63% (was 795 x 447) - Click image to enlargePosted Image


13. You can also customize the launch conditions:

Posted Image

Resized to 63% (was 795 x 447) - Click image to enlargePosted Image


14. When you're done with project customizations, build the project (right click on 'Setup1', then select the 'Build' option):

Posted Image

Now you've got a new installation package for your application or component.

This tutorial showed the basics of creating a setup project, so now you can easily deploy your applications to the end-user.

[教學]使用Visual Studio 2005製作應用程式的安裝檔

       寫應用程式時,如果引用了許多週邊的函式庫或dll檔案,在將程式的成品搬移至其他電腦執行時,是不是常發生東漏一個圖檔、西漏一個dll的情況呢?對於使用Visual Studio來寫程式的設計者,可以用它內建的「佈署專案」來製作整個程式的安裝檔,將你專案的程式及所需的檔案包裝在一個msi檔中,如此一來只要設定好佈署專案的內容,就不必擔心每次要帶到其他電腦上執行時會有缺漏了。
       以下便配合圖片來介紹如何使用佈署專案,使用的版本是Visual Studio 2005 Service Pack1
我們先拿一個範例專案MyProgram,來當做成品的應用程式,在這裡它是一個以C#寫成的Windows Form應用程式,輸出檔為MyProgram.exe。

        在這個專案中,我們參考了.NET的Managed DirectX,如上圖的Microsoft.DirectX、Microsoft.DirectX.Direct3D、Microsoft.DirectX.Direct3DX三項,另外還參考了一個自己找到的MgdWM4Wrapper,它是一個名為MgdWM4Wrapper.dll的dll檔案。

        接著在最上面的方案(也叫MyProgram)接右鍵,選擇加入->新增專案




      選擇左邊的其他專案類型,再選擇右邊的安裝專案,接著在下方的名稱輸入自己要的專案名稱,它也會是最後產生出來的msi檔的預設名稱。

      專案建立完成後,會自動開啟出該專案的設定檔案系統的頁面:




     這個頁面的介面基本上就跟檔案總管的型式是類似的,左邊是資料夾,右邊是資料夾的內容。在佈署專案的檔案系統中,有三個資料夾的內容要設定,分別是「使用者的桌面」、「使用者的程式功能表」--分別可以設定要在使用者的桌面和程式集功能表中加入哪些檔案(通常是捷徑),以及「應用程式資料夾」--存放應用程式所需檔案的地方,通常就是使用者電腦的「C:\Program Files」資料夾。在這裡我們先從「應用程式資料夾」一項開始設定,因為上二項的捷徑會需要對應至它其中的檔案。

    在左邊的應用程式資料夾上按右鍵,移到加入之上,會有四類的東西可以選擇:



      如果你希望在程式的資料夾中,再多建幾個資料夾以分類檔案,可以選擇加入資料夾。在這裡我們先選擇最重要的專案輸出



        在這個視窗中,最上面的專案下拉式列表可以讓你選擇同在一個方案中的專案,以本例而言僅有MyProgram專案。中間的視窗可以讓你選擇該專案中的哪些部份要加入至佈署中,可以配合Ctrl或Shift做複數選取,通常選擇主要輸出即可。 再下面的組態這是選擇該專案是要以什麼樣的建構版本加入至佈署(Debug或Release),預設是作用中,即跟從該專案現在的設定。

        按下確定後,我們可以發現除了MyProgram的主要輸出(即MyProgram.exe檔)外,其他額外的參考組件也被加入至應用程式資料夾中了,同時在「方案總管」中,SetupProgram專案的「偵測到的相依性」也列出了該四項組件。

                       

        除了專案輸出之外,如果應用程式有其他需要的檔案,也可以用加入->檔案的方式,加入至應用程式資料夾中。為了開發上的方便,最好是讓開發專案的資料夾及檔案配置,能與佈署專案中安排的配置一致,才不會開發時改一套,佈署時又要改一套。

       接下來我們設定一下應用程式資料夾的屬性,在「使用者的應用程式」的屬性視窗中,有二項需要注意的屬性:


      佈署專案的每個資料夾都會有AlwaysCreate屬性需要設定,它表示「即使該資料夾是空的,是否仍要建立」,預設值為False。通常是不會需要設定成True,但若你的專案有需要事先建立空白的資料夾時,可以把該資料夾的屬性設定為True。另一個屬性是DefaultLocation,它代表了在使用者的電腦中的位置,預設值是  [ProgramFilesFolder][Manufacturer]\[ProductName] ,我們以常用的軟體Adobe Reader的路徑為例:
C:\Program Files\Adobe\Reader 8.0

[ProgramFilesFolder]  ->  C:\ProgramFiles
[Manufacturer]   -> Adobe
[ProductName] -> Reader 8.0

如果要照這個預設值來安裝應用程式的話,後續的佈署專案屬性設定中可以設定Manufacturer及ProductName的值。


       應用程式資料夾的設定完成後,我們就可以來設定要在使用者的程式集和桌面上擺入哪些捷徑。對於每一個在應用程式資料夾中,你想對其設置捷徑的檔案(通常是應用程式的執行檔),在它上面按右鍵,選擇建立xxxx的捷徑,接著將其重新命名為你希望使用者看到的名稱:



命名後,將其拖曳至左方的「使用者的桌面」資料夾。對於「使用者的程式功能表」資料夾也如法炮製(捷徑要重製作一個),不過建議先在「使用者的程式功能表」下建立好公司名稱與程式名稱的資料夾,再將捷徑放進去;否則捷徑便會擺在使用者的程式集中的最外層。



      捷徑都設置完畢後,我們便可以來對佈署專案做最後的屬性設定(其實這部份要最先做也可以啦)


     這裡比較需要記得設定的屬性有:
Author -> 應用程式的作者
Manufacturer -> 應用程式的開發公司
ProductName -> 應用程式的名稱         如上面所提過,這二個屬性可以決定程式安裝的路徑
RemovePreviousVersions -> 當發現使用者的電腦中有較舊版本的同一應用程式時,是否要先移除舊版本
Title -> 安裝程式的標題
Version -> 指定安裝程式的版本,可以配合RemovePreviousVersions屬性來決定與舊版本程式的配合。

     除了屬性視窗中列出的屬性,其實佈署專案還有一個屬性頁面要設定,在方案總管中,於佈署專案的名稱上按右鍵,選擇屬性,會開啟下面這個視窗:



組態同樣可以設定佈署專案是要以Debug還是Release模式建置;輸出檔名稱可以設定msi檔的名稱;套件檔案可以選擇要將應用程式的檔案包含在msi檔、或是另外用一個cab檔包著、或是鬆散的檔案。若選擇前二者,則還可以設定壓縮是要以速度還是大小為優先做考量。最後按下必要條件進入最後的設定頁面:



        在這個視窗中,Visual Studio 2005會先幫你挑選好它偵測到的應用程式所需的必要元件,如本例中為.NET Framework 2.0。如果你擔心使用者電腦的Windows Installer可能版本不夠新或是損壞,可以再勾選Windows Installer 2.0或3.1版。最下面的三種選項,是設定安裝程式會在使用者的電腦上如何安裝這些必要元件,最保險的方法是從應用程式的相同位置下載必要條件,這會將這些必要元件的可轉散發安裝檔一起放在安裝程式中,缺點自然是整個安裝程式的大小會變得龐大,優點則是可以確保即使目標電腦沒有網路也能安裝這些必要元件。
       =================== 2010.02.03新增=====================
        若要在使用者的登錄檔(註冊機碼)系統中新增欄位,則可在「方案總管」中,於佈署專案上按右鍵,移到View(檢視),選擇「登錄」,即可看到註冊機碼列表,在左邊選擇對應的資料夾後,在右邊的視窗按右鍵選擇新增,即可增加註冊機碼。
        除了登錄檔外,也可以發現還有其他可供設定的選項,例如將使用者電腦中的特定副檔名與程式做連結調整安裝精靈的介面…等,這些東西個人尚未研究過,有興趣的朋友們可以自行嘗試看看
       ==================================================
        最後一步,在方案總管中,於佈署專案的名稱上按右鍵,選擇建置,就會開始產生安裝程式囉!視你所選擇的建置模式而異,可以到佈署專案的輸出資料夾下去看看有哪些檔案,以本例來看,檔案是放在Visual Studio 2005的Projects資料夾中,\MyProgram\SetupMyProgram\Debug   資料夾下:


不論剛剛的專案屬性選擇為何,都會有一個setup.exe檔案,通常它就是整個安裝程式的起點,會在安裝應用程式前檢查使用者的電腦是否有安裝應用程式的必要元件,如果沒有,則會開始下載並安裝必要元件(以上圖來看,因設定了從應用程式的相同位置下載必要條件,故會從一同產生的dotnetfix資料夾來安裝)。若不需要考慮必要元件,且當初「套件檔案」是選擇含在安裝檔中,則其實可以只拿msi檔去使用者的電腦安裝。除了安裝,Windows Installer其實連移除的工作也都幫我們設定好了,使用者之後也可以從控制台中的「新增/移除程式」來移除你的應用程式,實在是相當方便。

       有關佈署專案的教學就到這邊,其實還有一些小細節設定可以調整,就讓各位高手們去自己試試啦。

2011年9月13日 星期二

Android 產生自己的 keystore 金鑰步驟

1. 使用 命令提示字元 (cmd.exe) 切換到 java 目錄的 /jre/bin 下(例如:"C:\Program Files\Java\jdk1.6.0_21\jre\bin")

2.之後輸入 keytool -genkey -alias “keystore 路徑 ” -keyalg RSA -validity 20000 -keystore “keystore 路徑

3.然後會有一系列的東西需要你輸入,輸入完畢 金鑰就產生完成 ^^.

2011年9月12日 星期一

ImageView.ScaleType

結構
繼承關係
public static final enum ImageView.ScaleType extends Enum<extends Enum<E>>

java.lang.Object
java.lang.Enum<E extends java.lang.Enum<E>>
android.widget.ImageView.ScaleType

類概述
將圖片邊界縮放,以適應視圖邊界時的可選項。

常量
public static final ImageView.ScaleType CENTER         
在視圖中使圖像居中,不執行縮放。  XML 中可以使用的語法: android:scaleType="center" 

public static final ImageView.ScaleType CENTER_CROP     
均衡的縮放圖像(保持圖像原始比例),使圖片的兩個坐標(寬、高)都大於等於 相應的視圖坐標(負的內邊距)。圖像則位於視圖的中央。  XML 中可以使用的語法:android:scaleType="centerCrop" 

public static final ImageView.ScaleType CENTER_INSIDE  
均衡的縮放圖像(保持圖像原始比例),使圖片的兩個坐標(寬、高)都小於等於 相應的視圖坐標(負的內邊距)。圖像則位於視圖的中央。  XML 中可以使用的語法:android:scaleType="centerInside" 

public static final ImageView.ScaleType FIT_CENTER
使用 CENTER 方式縮放圖像。  XML 中可以使用的語法: android:scaleType="fitCenter" 

public static final ImageView.ScaleType FIT_END
使用 END 方式縮放圖像。  XML 中可以使用的語法: android:scaleType="fitEnd" 

public static final ImageView.ScaleType FIT_START
使用 START 方式縮放圖像。  XML 中可以使用的語法:android:scaleType="fitStart" 

public static final ImageView.ScaleType FIT_XY 
使用 FILL 方式縮放圖像。  XML 中可以使用的語法: android:scaleType="fitXY" 

public static final ImageView.ScaleType MATRIX
繪製時,使用圖像矩陣方式縮放。圖像矩陣可以通過 setImageMatrix(Matrix) 設置。在 XML 中可以使用的語法: android:scaleType="matrix" 

補充
         文章精選
                   Android APIImageView.ScaleType代碼演示