2012年1月9日 星期一

VS2005 製作安裝程式-自動移除舊版程式

如果你用 VS2005 的 Setup 專案來佈署你的應程式,預設情況下當你製作新版的安裝程式,並在執行安裝程式應該會出現下面的畫面(VS2005環境中的安裝不會)


如要解決這個問題,將 Setup 專案屬性的 RemovePreviousVersions 設為 true,並將版本編號遞增比原來版本高,如下圖:

如此可解決上述新版安裝問題

2012年1月8日 星期日

C# 讀檔

using System.IO;
using System.Text;

FileStream fs = new FileStream("c:\test.txt",FileMode.Open);
StreamReader sr = new StreamReader(fs,Encoding.Default);
string s1 = "";
while((s1=sr.ReadLine()) != null)
{
...
}

2012年1月2日 星期一

c# setup uninstall

1. 建立 Simple Project UninstallApps.
2. 在 Main() 下加入以下 Coding

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;

namespace UninstallCalc
{
    static class uninstall
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string[] arguments = Environment.GetCommandLineArgs();

            foreach (string argument in arguments)
            {
                string[] parameters = argument.Split('=');
                if (parameters[0].ToLower() == "/u")
                {
                    string productCode = parameters[1];
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
                    Process proc = new Process();
                    proc.StartInfo.FileName = string.Concat(path, "\\msiexec.exe");
                    proc.StartInfo.Arguments = string.Concat(" /i ", productCode);
                    proc.Start();
                }
            }
        }
    }
}


3. 建立新的 Setup Project
4. 新增 UninstallApp.exe 到 'File System' 內的 "Application Folder"
5. 再於 "User's Program menu" 建立一個 UninstallApp.exe 的捷徑,並於內容的 "Arguments" 參數中加入 "/u=[ProductCode]".
    a. 於 Deployment Project 中的 Properties 內可找到 ProductCode
    b.例:Arguments               /u={B126BC4C-F061-4E54-A0D0-500892F2FEDB}
6. 最後重建 Deployment Project