Apr 18

剛感到好奇,各在 C# 與 JAVA 寫測試程式。

  • C#:
    Object myObj1 = 1;
    Object myObj2 = myObj1;
    Object myObj3 = 1;
    
    Console.WriteLine("myObj2.Equals(myObj1) = " + myObj2.Equals(myObj1));
    Console.WriteLine("myObj3.Equals(myObj1) = " + myObj3.Equals(myObj1));
    
    Console.WriteLine("(myObj2 == myObj1) = " + (myObj2 == myObj1));
    Console.WriteLine("(myObj3 == myObj1) = " + (myObj3 == myObj1));

    結果:

    myObj2.Equals(myObj1) = True
    myObj3.Equals(myObj1) = True
    (myObj2 == myObj1) = True
    (myObj3 == myObj1) = False
  • JAVA:
    Object myObj1 = 1;
    Object myObj2 = myObj1;
    Object myObj3 = 1;
    
    System.out.println("myObj2.equals(myObj1) = " + myObj2.equals(myObj1));
    System.out.println("myObj3.equals(myObj1) = " + myObj3.equals(myObj1));
    
    System.out.println("(myObj2 == myObj1) = " + (myObj2 == myObj1));
    System.out.println("(myObj3 == myObj1) = " + (myObj3 == myObj1));

    結果:

    myObj2.equals(myObj1) = true
    myObj3.equals(myObj1) = true
    (myObj2 == myObj1) = true
    (myObj3 == myObj1) = true

Technorati Tags: ,

(Visited 3254 times)
Jan 04

前陣子,在工作上剛好需要對 wav 檔案格式進行判定,可是 NAudio 製作出來的格式用來作判斷又有錯誤,所以土法煉鋼寫了一個小 class:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class WavInfo
{
    private String _Error;
    public String Error
    {
        get
        {
            return this._Error;
        }
    }

    private Boolean _PCM;
    public Boolean PCM
    {
        get
        {
            return this._PCM;
        }
    }

    private uint _Channel;
    public uint Channel
    {
        get
        {
            return this._Channel;
        }
    }

    private uint _SampleRate;
    public uint SampleRate
    {
        get
        {
            return this._SampleRate;
        }
    }

    private uint _BitsPerSample;
    public uint BitsPerSample
    {
        get
        {
            return this._BitsPerSample;
        }
    }

    private uint _ByteRate;
    public uint ByteRate
    {
        get
        {
            return this._ByteRate;
        }
    }

    private uint _BlockAlign;
    public uint BlockAlign
    {
        get
        {
            return this._BlockAlign;
        }
    }

    public WavInfo(String FileName)
    {
        try
        {
            FileStream FS = File.OpenRead(@FileName);
            Byte[] ReadTmp = new Byte[36];

            FS.Read(ReadTmp, 0, 36);
            FS.Close();

            if (BitConverter.ToUInt16(ReadTmp, 20) == 1)
            {
                this._PCM = true;
            }

            this._Channel = BitConverter.ToUInt16(ReadTmp, 22);
            this._SampleRate = BitConverter.ToUInt32(ReadTmp, 24);
            this._BitsPerSample = BitConverter.ToUInt16(ReadTmp, 34);

            this._ByteRate = this._Channel * this._SampleRate * this._BitsPerSample / 8;
            this._BlockAlign = this._Channel * this._BitsPerSample / 8;

            this._Error = String.Empty;
        }
        catch (Exception e)
        {
             this._Error = e.ToString();
        }
    }
}
  • 檢查 Error 是否為空字串,就知道是否成功取得 wav 檔案資訊。
  • 檢查 PCM 是否為 true,就知道該檔案是不是 PCM 格式的 wav 檔。
  • wav 檔播放的時間會等於 ByteRate。

Technorati Tags: , , , , , ,

(Visited 4838 times)
Oct 11

這是我的第一個 C# Windows Form 程式。

執行畫面:
puttyBackup

使用方法:

  1. 點選 Browse 按鈕,選擇並輸入 reg 檔存放位置與檔名。
  2. 點選 Backup 按鈕就能將 putty 的登錄檔輸出。

下載:

Technorati Tags: , , , ,

(Visited 6379 times)
Oct 08

最近在玩 C#,寫個小軟體。
不過,我發現我產生出來的 exe 檔就是很醜,看起來的感覺就好像是病毒檔... :oops:
用 MSDN 跟上網搜尋,solution 也不算好找,所以寫這篇順便幫自己紀錄一下。

Windows Form 有兩種圖示可以設定:

  • Form 的圖示:只要在 Form 的屬性中,更改 Icon 屬性的 Value 就好,設定的圖示會顯示在執行的視窗的左上角。
  • Application 的圖示:在 Visual Studio 2008 中,最上方工具列的 專案 -> [專案名稱]屬性,開啟的分頁中的 應用程式 有個 圖示與資訊清單 項目。在此便可設定軟體圖示。

By the way,我覺得目前寫這個小軟體應該還算實用,寫完後再丟上來~ 8)

Technorati Tags: , ,

(Visited 6767 times)
Jun 22

剛剛弄出來的東西.

DsExport.vb :

Public Class DsExport

  Public Shared Function Export(ByVal ds As DataSet, _
                                ByVal dtName As String, _
                                ByVal colList() As String, _
                                ByVal colValue() As Integer) As String
    Dim header As String = ""
    Dim body As String = ""
    Dim record As String = ""

    For Each col As String In colList
      header = header & Chr(34) & col & Chr(34) & ","
    Next
      header = header.Substring(0, header.Length - 1)

    For Each row As DataRow In ds.Tables(dtName).Rows
      Dim arr() As Object = row.ItemArray()

      For Each i As Integer In colValue
        If arr(i).ToString().IndexOf(",") > 0 Then
          record = record & Chr(34) & arr(i).ToString() & Chr(34) & ","
        Else
          record = record & arr(i).ToString() & ","
        End If
      Next

      body = body & record.Substring(0, record.Length - 1) & vbCrLf
      record = ""
    Next

    Return header & vbCrLf & body

  End Function

End Class

呼叫方式:

  Private Sub BtnExport_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles BtnExport.Click
    Dim colList() As String = {"CSV表頭1", "CSV表頭2", "CSV表頭3"}

    "顯示順序: SQL query 刮出來的欄位0, 4, 2
    Dim colValue() As Integer = {0, 4, 2}

    Dim strData As String = _
      DsExport.Export(DataSet 變數名稱, _
                      "DataSet 變數中的 DataTabel 名稱", _
                      colList, colValue)

    Dim binData() As Byte = System.Text.Encoding.Default.GetBytes(strData)

    Response.Clear()
    Response.AddHeader("Content-Type", "application/vnd.ms-access")
    Response.AddHeader("Content-Disposition", "inline;filename=檔案名稱.csv")
    Response.BinaryWrite(binData)
    Response.End()
  End Sub

Technorati Tags: , , ,

(Visited 7639 times)