顯示具有 檔案處理 標籤的文章。 顯示所有文章
顯示具有 檔案處理 標籤的文章。 顯示所有文章

2014年5月19日 星期一

C# 取得程式路徑的幾個方法

1.獲取和設置當前目錄的完全限定路徑。
string str = System.Environment.CurrentDirectory;


Result: C:\xxx\xxx


2.獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
string str = System.Windows.Forms.Application.StartupPath;

Result: C:\xxx\xxx


3.獲取新的 Process 組件並將其與當前活動的進程關聯的主模塊的完整路徑,包含文件名。

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

Result: C:\xxx\xxx\xxx.exe


4.獲取當前 Thread 的當前應用程序域的基目錄,它由程序集衝突解決程序用來探測程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;

Result: C:\xxx\xxx\


5.獲取應用程序的當前工作目錄。

string str = System.IO.Directory.GetCurrentDirectory();

Result: C:\xxx\xxx


6.獲取和設置包含該應用程序的目錄的名稱。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

Result: C:\xxx\xxx\


7.獲取當前進程的完整路徑,包含文件名。
string str = this.GetType().Assembly.Location;

Result: C:\xxx\xxx\xxx.exe


8.獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。string str = System.Windows.Forms.Application.ExecutablePath;

Result: C:\xxx\xxx\xxx.exe

2012年1月10日 星期二

C# 嵌入檔案,包在EXE裡

圖檔可以透過加入Resource的方式來使用,那其他檔案呢?有一種方便的嵌入方式可以把想要的檔案夾在你產生的EXE中,從專案中選擇「加入」、「現有項目」、然後從檔案對話框當中更改檔案類型為*.*,點選你想要的檔案,可加入專案中。

  




接下來,在方案總管中選擇該檔案,在屬性視窗將「建置動作」改為「內嵌資源」,這樣就可以讓程式碼引用,程式碼如下。










                    // 從專案組件讀入檔案到磁碟
                    Assembly Asmb = Assembly.GetExecutingAssembly( );
                    Stream ManifestStream = Asmb.GetManifestResourceStream( Asmb.GetName( ).Name + ".Update_Downloader.exe" );


                    // 讀入檔案
                    byte[] StreamData = new byte[ ManifestStream.Length ];
                    ManifestStream.Read( StreamData, 0, (int)ManifestStream.Length );


                    // 存到磁碟
                    FileStream FileStm = new FileStream( ExeFileName, FileMode.Create );
                    FileStm.Write( StreamData, 0, (int)ManifestStream.Length );
                    FileStm.Close( );

2012年1月6日 星期五

C# 透過HTTP抓檔案回來

在Window視窗上放一個Button,一個ProgressBar,一個TextBlock(用來顯示ProgressBar的數值)
利用以下的函式,可以完成


        private void Btn_http_Click( object sender, RoutedEventArgs e )
        {
            // 非同步
            WebClient MyWebClient = new WebClient( );
            MyWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler( Http_Completed );
            MyWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler( Http_ProgressChanged );
            MyWebClient.DownloadFileAsync( new Uri( "http://www.joyplux.net/xplayer/download/JoyeDM/JoyeDMDownload_Setup.exe" ), @"c:\temp\JoyeDM.exe" );
        }


        private void Http_ProgressChanged( object sender, DownloadProgressChangedEventArgs e )
        {
            PgBar_Http.Value = e.ProgressPercentage; //控制ProcessBar顯示進度
            TxtBk_Percent.Text = e.ProgressPercentage + "%";
        }


        private void Http_Completed( object sender, AsyncCompletedEventArgs e )
        {
            MessageBox.Show( "Download completed!" );
        }

C# 取得檔案版本資訊

FileVersionInfo.GetVersionInfo(@"C:\WINDOWS\NOTEPAD.EXE").FileVersion.ToString();