2014年5月7日 星期三

[C#] 新增、刪除、取得 Windows 系統登錄檔 (Registry)

  在 Windows programming 中,經常會利用系統登錄檔 (Registry) 儲存應用程式 (Application) 相關組態 (Configuration) 等資訊。以下文章將示範如何使用 C# 進行系統登錄檔 (Registry) 的新增、刪除和修改。

在 C# 中對系統登錄檔 (Registry) 操作必須先載入相關 namespace:

using Microsoft.Win32;

接下來將以 Current User 下的 SOFTWARE 為例,進行操作的講解。

新增
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.SetValue(registryName, registryValue); //建立 Name/Value
regKey.close();


刪除
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.DeleteValue(registryName); //刪除 registryName
regKey.close();


取得
string registryPath = @"SOFTWARE"; //HKEY_CURRENT_USER\Software
//開啟 Software 子系統登錄檔
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(registryPath);
regKey.GetValue(registryName); //取得 registryName
regKey.close();




參考資料:MSDN

2014年3月10日 星期一

[Ubuntu, Git, GitHub] 在 Ubuntu 12.04 上使用 GitHub

最近看了些新聞以及求才訊息,發現不少公司都將 GitHub 視為工程師加分的一個條件。

在公司內,專案開發使用的版本控制系統為 Subversion

趁著撰寫軟體驗證 (Proof Of Concept) 的時候,將自己的程式碼上傳至並學習 GitHub。

筆者大多數的程式碼皆在 Ubuntu 上進行開發,故下列文章內容將以 Ubuntu 12.04 作為範例。

首先,GitHub 仍然是以 Git 為基礎的,所以必須在 Ubuntu 12.04 上安裝 Git

sudo apt-get install git

接下來,必須對 Git 進行配置 (configure)

git config --global user.name "Your GitHub Username"
git config --global user.email "Your GitHub 註冊 Email"

如果,今天你已經有現存的 repository,請先複製 (clone) 下來

git clone https://github.com/Your GitHub Username/Your Repository Name.git

接著,可在您複製的資料夾內進行專案管理。

如果您新增了一份檔案,最終您必須將檔案新增至 repository。

git add Your New File

新增完畢,將檔案 commit 至 repository

git commit -m 'Your commit message'

最後,將您修改的檔案 push 回 GitHub

git remote add origin https://github.com/Your GitHub Username/Your Repository Name.git

git push origin master ( 輸入您的帳號與密碼 )

完成上述流程,一切就大功告成囉!

2014年1月27日 星期一

[Python, Socket] Error Code: 98 Message Address Already in Use

最近在開發一些 Proof of Concept 的程式,使用到 Python 的 socket。

Socket server / client 的開發上並未遇到太大的問題,倒是後續的使用上時常遇上

Bind failed. Error Code: 98 Message Address already in use 這問題。

經過 Google 大神的指示,小弟透過這裡紀錄解決方法,以便後續查找!

問題發生的主因,似乎是 socket server 在 listen 的 port 被占住了!導致系統回報錯誤。

首先使用

sudo netstat -ltnp | grep ':YOUR PORT NUMBER'

上述的指令會回覆:

tcp   0   0 0.0.0.0:YOUR PORT NUMBER   0.0.0.0:*   LISTEN   30381/python

由此回覆,我們發現 YOUR PORT NUMBER 正使用 process 30381 ( 表示使用中 )

這時候把這一個 process kill 掉就可以解決問題囉!

sudo kill -9 30381

2014年1月22日 星期三

[Android] 如何改變 application 啟動的第一個 Activity (How to change app's starting activity?)

先前在開發 Android 的時候,遇上了一個很囧的問題。

就是新開發的 activity ( 不是新創專案就有的那個 activity ),

希望將它視為 application 一啟動後就載入的第一個 activity。

當然,遇到問題的第一步就是‧‧‧詢問 Google 大神囉!

找到的詳解為:

在專案的 AndroidManifest.xml 檔案中,將您預想 launch 的那一個 activity 修正為

<activity android:name=".Your app's starting activity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />                 </intent-filter>
</activity>


參考資料:http://ppt.cc/OpKg

2013年10月17日 星期四

[Apache] 在 Ubuntu 12.04 中修改 Apache 的 (土阜) 號 (port number)

[Django] Installing Django with Apache & mod_wsgi 中曾經介紹一部分的 Apache 設定。基本上, Apache 原始使用 80 (土阜) 作為網站的入口。可是問題來了‧‧‧如果不想使用 80 port,而是想使用其他的 port number 來作為網站的入口,我該怎麼做呢?

答案是,!!!

首先,找到 /etc/apache2/port.conf 這個檔案,並修改這個檔案如下:

NameVirtualHost *: PORT NUMBER
Listen PORT NUMBER


接著,將 /etc/apache2/site-available/default 這個檔案中的 <VirtualHost *:80> 修改為:

<VirtualHost *: PORT NUMBER>

最後,重新啟動 Apache 就大功告成囉!

sudo service apache2 stop
sudo service apache2 start

想驗證設定成功與否,打開瀏覽器並鍵入 http://localhost:PORT NUMBER。如果出現網站的首頁即表示修改 (土阜) 號成功!

2013年10月5日 星期六

[Ubuntu] 在 Ubuntu 12.04 搭建 PXE 伺服器

下面是最近實驗的一些紀錄。

在 Ubuntu 12.04 上搭建 PXE 伺服器必須安裝下列 packages:

1. inetutils-inetd
2. tftpd-hpa
3. isc-dhcp-server

安裝完畢後,分別對 TFTP, DHCP, inetd 進行組態。

TFTP

1. 編輯 /etc/default/tftpd-hpa 檔案,在原始檔案內加入:

RUN_DAEMON="yes"
OPTIONS="-l -s /var/lib/tftpboot"

2. 重啟 TFTP

/etc/init.d/tftpd-hpa restart

DHCP 

1. 編輯 /etc/dhcp/dhcpd.conf,在原始檔案內加入: 

1-1. 如果原始環境已存在 DHCP 伺服器,請在原始設定中加入【以下為範例】

subnet 192.168.1.0 netmask 255.255.255.0 {
    < 原有的設定 >
    filename "pxelinux.0";
    next server < PXE 伺服器 >;
}

1-2. 如果原始環境未存在 DHCP 伺服器,請參照下列範例進行設定。

subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.3 192.168.1.255;
    filename "pxelinux.0";
}

inetd 

1. 編輯 /etc/inetd.conf,在原始檔案末端加入:

tftp dgram udp wait root /usr/sbin/in.tftpd /usr/sbin/in.tftpd -s /var/lib/tftpboot 

完成上述 packages 以及設定後,這台伺服器將擁有 PXE 的功能。

2013年6月5日 星期三

[Ubuntu] 在 Ubuntu 12.04 上設定無線網路

  由於實驗上的需求,必須使環境內的伺服器擁有對外網路的功能。但是,實驗室內的網路皆為私有網路,沒有對外存取的功能,因此必須藉由無線網路獲得此項功能。

首先,將無線網卡與伺服器連接。

按照下述步驟即可完成「無線網路」設定:

STEP 1. 尋找無線網卡代號 iwconfig 〈以本次實驗機器為例:wlan0〉

STEP 2. 啟用無線網卡
sudo ifconfig wlan0 up

STEP 3. 尋找可用無線 AP 〈Access Point〉
sudo iwlist wlan0 scanning

藉由上述指令可找出附近可用的無線 AP,並記下它們的 ESSID

STEP 4. 連接無線 AP
sudo iwconfig wlan0 essid "AP_NAME"

STEP 5. 取得 IP
sudo dhclient wlan0

如果是不需輸入密碼的無線 AP,完成上述步驟即可上網。

如果需輸入密碼,請接續下列步驟:
sudo iwconfig wlan0 key ********