2014年9月20日 星期六

[C#] Run windows command

要執行如下的Windows command (Unpack tar and gz files)

start "" "C:\Program Files\7-Zip\7z.exe"
"C:\Program Files\7-Zip\7z" x "D:\ Files\*.tar" -o" D:\ Files" > err.log
"C:\Program Files\7-Zip\7z" x " D:\ Files\*.gz" -o" D:\ Files" > err.log


String cmd1 = String.Format("start \"\" \"{0}\"",Path.Combine(ZIP_PATH, "7z.exe"));
String cmd2 =
String cmd3 = …

1.  方法一 寫入bat檔案,再執行bat

String batFileName = "UnpackTar.bat";
StreamWriter sw;
sw = new StreamWriter(batFileName, false);
sw.WriteLine(cmd1);
sw.WriteLine(cmd2);
sw.WriteLine(cmd3);
sw.Close();

var p = Process.Start(batFileName);
p.WaitForExit(); //等待Process執行完畢

2.  方法二 直接起CMD.exe執行windows command line

String strCmdText = String.Format("{0}{1} & {2} & {3}", "/C ", cmd1, cmd2, cmd3);
var p = Process.Start("CMD.exe", strCmdText);
p.WaitForExit(); //等待Process執行完畢

注意在command前面要加個”/C ”

3.  方法三 直接起CMD.exe執行windows command line (Hide CMD window)

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = String.Format("{0}{1} & {2} & {3}", "/C ", cmd1, cmd2, cmd3);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(); //等待Process執行完畢

ð  利用”&” 來將三個指令合併成one line command



Reference


查詢Sql Server版本

1.  列出Sql Server版本號碼

USE master
GO
SELECT
SERVERPROPERTY('ProductVersion') N'執行個體的版本',
SERVERPROPERTY('ProductLevel') N'執行個體的版本層級',
ATABASEPROPERTYEX('master','Version') N'資料庫

版本號碼';


查詢結果




2.  列出Sql Server版本

Select @@version


查詢結果 :
       Microsoft SQL Server 2012 - 11.0.2100.60 (X64)
        Feb 10 2012 19:39:15
        Copyright (c) Microsoft Corporation

  Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

2014年9月16日 星期二

Mongo DB repository for CRUD (2)

Mongo DB repository for CRUD (2)

A mongo worker for inheritance

public abstract class MongoWorker<T> : IDisposable
    {
        protected MongoServer _server = null;
        protected MongoClient _client = null;
        protected MongoDatabase _database = null;
        protected MongoCollection _collection = null;


        public MongoWorker(MongoConnTestPool mongoConnection, String collectionName)
        {
            if (mongoConnection == null || String.IsNullOrEmpty(collectionName))
            {
                throw new Exception("Mongo connect string and collection name must be provided!");
            }
            this._client = new MongoClient(mongoConnection.GetConnectionString());
            this._server = this._client.GetServer();
            this._database = _server.GetDatabase(collectionName);
            this._collection = _database.GetCollection<T>(collectionName);
            this._server.Connect();
        }

        public abstract IQueryable<T> Get();

        public abstract void Insert(T doc);

        public abstract void Delete(T doc);

        public abstract int Update(T doc);

        public virtual void Dispose()
        {
            try
            {
                if (this._server.State.Equals(MongoServerState.Connecting) ||
                    this._server.State.Equals(MongoServerState.Connected))
                {
                    this._server.Disconnect();
                }

                if(this._server!=null)
                {
                    this._server = null;
                }
                if (this._client != null)
                {
                    this._client = null;
                }
                if (this._database != null)
                {
                    this._database = null;
                }
                if (this._collection != null)
                {
                    this._collection = null;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }






2014年9月1日 星期一

[Linux cmd] Compress and Extract file with tar.gz

1.      解開一個Tar file
$ tar xvf [XXX.tar]

解壓縮到指定路徑(資料夾)
$ tar xvf [XXX.tar] -C [directory path]


2.      加密檔案成Tar
$ tar cvf [Tar file name] [Original file name]

3.      解開gz file
$ gunzip [XXX.gz] -C [directory path]

解開所有資料夾下的gz
$ gunzip *.gz

4.      加密檔案成gz
$ gzip [File name]

5.      Reference