2012/02/25

C# 取得 windows 下TEMP目錄


To get the temp path in c#, the most used option I have seen is :

string tempPath = Environment.GetEnvironmentVariable("Temp");

which works ok most of the time, but personally I don't like to pass string variables around,
so I went looking for something as :
string tempPath = Environment.GetFolderPath(SpecialFolder.Temp);
which ofcourse does not exist.

But as with google as a friend, I found the best way (for me) :

String tempPath = Path.GetTempPath();

(always in the last place you look of course ;-) )

出處

2012/02/21

(C#)使用命令列參數(Command Line Arguments)

命令列參數是什麼?

就是假設我們有一個exe的執行檔,

點它產生一個捷徑後,再點捷徑右鍵,

會看到一段路徑像是

D:\vs2008\commandLineArgTest\bin\Debug\commandLineArgTest.exe

此時我們可以在上面的路徑的最後加上空白然後welkin然後空白然後Naruto

D:\vs2008\commandLineArgTest\bin\Debug\commandLineArgTest.exe welkin Naruto

就可以把welkin跟Naruto這兩個參數傳到commandLineArgTest.exe程式裡面

 

先創一個C#的視窗應用程式

然後打開Program.cs

在static void Main(string[] CoolArgs)裡面加上一個我們要傳進來的陣列

此陣列就是welkin跟Naruto,他們會以空白為區隔

 

然後在static void Main裡面呼叫Application.Run(new Form1(CoolArgs));時

在把CoolArgs這個參數傳到Form1里面

 

然後在Form1里面寫上一些程式

   public partial class Form1 : Form
    {
        string[] myArgs;

        public Form1(string[] args)
        {
            InitializeComponent();

            //把從外面傳來的args給myArgs
            myArgs = args;

            //然後就呼叫我們下面自己寫的函式去show出陣列的每一個內容
            showArgs();
            
        }

        public void showArgs()
        {
            for (int i = 0; i < myArgs.Length; i++)
            {
                MessageBox.Show(myArgs[i]+Environment.NewLine);
            }
            
        }
    }

 

如果再Debug時沒有命令列參數可以打的話怎麼辦??

先開C#專案的Properties頁面

然後選Debug頁面

就有一個Command line arguments可以打啦

用空白當成每一個陣列元素的分隔喔



出處 http://welkingunther.pixnet.net/blog/post/28282317-%28c%23%29%E4%BD%BF%E7%94%A8%E5%91%BD%E4%BB%A4%E5%88%97%E5%8F%83%E6%95%B8%28command-line-arguments%29