using System.Diagnostics; using System; namespace ReadMSMQ { class Program { static void Main(string[] args) { //只要改第一與第三個參數, [hostname]\private$\[queuename] PerformanceCounter objCounter1 = new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"testHost\private$\testQueue"); Console.WriteLine(objCounter1.NextValue().ToString()); //查出MSMQ中, 每個Queue的Messages數量, 可用下面的方式 PerformanceCounterCategory objCategory = new PerformanceCounterCategory("MSMQ Queue"); PerformanceCounter objCounter2 = new PerformanceCounter("MSMQ Queue", "Messages in Queue"); foreach (string strInstanceName in objCategory.GetInstanceNames()) { objCounter2.InstanceName = strInstanceName; Console.Write(strInstanceName + " = " + objCounter2.NextValue().ToString()); } } } }
2006/08/25
[C#]用PerformanceCounter讀取MSMQ中的訊息數量
[Sun AppServer]建立Sun AppServer與NodeAgent的Windows服務
使用Windows的sc.exe來完成Windows服務的建立.
- 建立一個 Sun Java System Application Server 的 Windows 服務.
C:\WINDOWS\system32\sc.exe create sun_domain1 binPath= "C:\Sun\ApplicationServer\lib\appservService.exe \"C:\Sun\ApplicationServer\bin\asadmin.bat start-domain --user admin --passwordfile C:\Sun\ApplicationServer\password.txt domain1\" \"C:\Sun\ApplicationServer\bin\asadmin.bat stop-domain domain1\"" DisplayName= "SUNONE AppServer domain1"- 若執行成功, 會出現 "[SC] CreateService 成功" 的訊息.
此 Windows 服務的顯示名稱為"SUNONE AppServer domain1", 服務名稱為 "sun_domain1".
若要刪除此 Windows 服務, 可用以下的指令:
C:\WINDOWS\system32\sc.exe delete sun_domain1
- 若執行成功, 會出現 "[SC] CreateService 成功" 的訊息.
[VirtualPC]解決PAE Error的方法
將 boot.ini 中的 [operating systems] 改為
multi(0)disk(0)rdisk(0)partition(2)\WINDOWS = "Microsoft Windows XP Professional" /fastdetect /execute /NOPAE
[Sun AppServer]Tomcat透過IIOP使用Sun AppServer的EJB
前置作業:
- 將 Sun AppServer 安裝目錄中的 lib\j2ee.jar 與 lib\appserv-rt.jar 分別放至 Tomcat 安裝目錄中的 common\lib 與 shared\lib 下
- 在 Web 專案中, 將 EJB Module 部署至 Sun AppServer 後產生的 xxxClient.jar 檔加入 WEB-INF\lib 中
[Java]讀取MS Access資料
//設定Connection的URL String filepath = "./test.mdb"; String url = "jdbc:odbc:DRIVER=Microsoft Access Driver (*.mdb);DBQ=" + filepath; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //取得Access的Connection Connection objAccessConnection = DriverManager.getConnection(url); DatabaseMetaData objMeta = objAccessConnection.getMetaData(); //取得Access的Table Schema ResultSet objTables = objMeta.getTables(null, null, null, new String[]{"TABLE"}); //取得Access中的所有表格 while (objTables.next()) { //表格名稱 String strTableName = objTables.getString("TABLE_NAME"); //查詢的SQL句 String strQuerySql = "SELECT * FROM " + strTableName; //查詢用的Statement Statement objAccessStatement = objAccessConnection.createStatement(); //取得資料集 ResultSet objData = objAccessStatement.executeQuery(strQuerySql); }
[Sun AppServer]建立sybase的ConnectionPool
以 Sybase 的 jConnect-6_0 為例:
- 將 jConnect-6_0 的 jar 檔 (jconn3.jar & jTDS3.jar) 複製到 $sunap\lib 下
- 建立 JDBC Connection Pool 的資料如下:
Datasource Classname: com.sybase.jdbc3.jdbc.SybDataSource
Properties:
databaseName: 資料庫名稱
serverName: 資料庫的主機名
PortNumber: 資料庫的 port (Ex: 5000)
User: 連線的使用者帳號
Password: 連線的使用者密碼
charset: 語系 (Ex: big5,utf-8)
[SQLServer2000]查出資料庫各表格的資訊
SELECT sysusers.name + N'.' + sysobjects.name as ObjectName, sysindexes.name as IndexName, sysindexes.rows, --資料列 case indid when 1 then 1 else 0 end as IsClusteredIndex, sysindexes.indid, --索引的識別碼 sysobjects.name, --物件名稱 sysusers.name --使用者名稱或群組名稱在資料庫中是唯一的 FROM sysusers, sysobjects, sysindexes WHERE sysusers.uid = sysobjects.uid and sysindexes.id = sysobjects.id and sysobjects.name not like '#%' and OBJECTPROPERTY(sysobjects.id, N'IsMSShipped') <> 1 and OBJECTPROPERTY(sysobjects.id, N'IsSystemTable') = 0 ORDER BY ObjectName, IsClusteredIndex DESC, indexproperty(sysindexes.id, sysindexes.name, N'IsStatistics'), IndexName
[C# & Java]GET/POST資料
[C#]
- GET:
//設定GET資料 Uri t_uri = new Uri("http://localhost/Test/Get.asp?id=yilin"); HttpWebRequest t_req = System.Net.WebRequest.Create(t_uri) as HttpWebRequest; t_req.Method = "GET"; //取得回應的內容 System.Net.WebResponse t_resp = t_req.GetResponse(); System.IO.StreamReader t_stream = new System.IO.StreamReader(t_resp.GetResponseStream()); Console.WriteLine(t_stream.ReadToEnd().Trim());
- POST:
string t_url = "http://localhost/Test/Post.asp"; string t_post = "id=yilin&password=helloworld"; //將POST資料轉成byte array byte[] t_bytdata = Encoding.UTF8.GetBytes(t_post); WebRequest t_req = WebRequest.Create(t_url); //設定POST資料 t_req.ContentType = "application/x-www-form-urlencoded"; t_req.Method = "POST"; Stream t_ws = t_req.GetRequestStream(); t_ws.Write(t_bytdata, 0, t_bytdata.Length); t_ws.Close();
[C#]在程式中模擬特定的windows帳號存取網路芳鄰
using System; using System.IO; using System.Runtime.InteropServices; using System.Security.Principal; using System.Security.Permissions; [assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode = true)] [assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")] namespace ConsoleApplication1 { class Class1 { //登入 [DllImport("advapi32.dll", SetLastError = true)] public static extern bool LogonUser( string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); //登出 [DllImport("kernel32.dll")] public extern static bool CloseHandle(IntPtr hToken); public Class1() { string UserName = "username"; string MachineName = "192.168.0.10"; string Pw = "password"; string IPath = @"\\" + MachineName + @"\shared"; const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_LOGON_NEW_CREDENTIALS = 9; IntPtr tokenHandle = new IntPtr(0); tokenHandle = IntPtr.Zero; //將登入的Token放在tokenHandle bool returnValue = LogonUser(UserName, MachineName, Pw, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref tokenHandle); //讓程式模擬登入的使用者 WindowsIdentity w = new WindowsIdentity(tokenHandle); w.Impersonate(); if (false == returnValue) { //登入失敗的處理 return; } //取得該目錄下的所有檔案名稱 DirectoryInfo dir = new DirectoryInfo(IPath); FileInfo[] inf = dir.GetFiles(); for (int i = 0; i < inf.Length; i++) { Console.WriteLine(inf[i].Name); } } } }附註
- 如果電腦存在於網域, LogonUser的第二個參數須用網域名:
LogonUser(UserName, "myDomain", Pw,LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);
- 相關參考網頁: http://www.thecodeproject.com/csharp/cpimpersonation1.asp