2007年1月29日月曜日

[VB.NET] 数値書式指定文字列

数値書式指定文字列について。

Dim i As Integer
i = 1234567890
Debug.WriteLine("C " & i.ToString("C"))
Debug.WriteLine("D " & i.ToString("D"))
Debug.WriteLine("D12 " & i.ToString("D12"))
Debug.WriteLine("E " & i.ToString("E"))
Debug.WriteLine("F " & i.ToString("F"))
Debug.WriteLine("F4 " & i.ToString("F4"))
Debug.WriteLine("G " & i.ToString("G"))
Debug.WriteLine("G4 " & i.ToString("G4"))
Debug.WriteLine("G12 " & i.ToString("G12"))
Debug.WriteLine("N " & i.ToString("N"))
Debug.WriteLine("N0 " & i.ToString("N0"))
Debug.WriteLine("N12 " & i.ToString("N12"))
Debug.WriteLine("12N " & i.ToString("12N"))
Debug.WriteLine("P " & i.ToString("P"))
'Debug.WriteLine("R " & i.ToString("R")) ' 無効
Debug.WriteLine("X " & i.ToString("X"))
Debug.WriteLine("X8 " & i.ToString("X8"))

Dim d As Double
d = 1234567890.0987654
Debug.WriteLine("C " & d.ToString("C"))
'Debug.WriteLine("D " & d.ToString("D")) ' 無効
Debug.WriteLine("E " & d.ToString("E"))
Debug.WriteLine("F " & d.ToString("F"))
Debug.WriteLine("F4 " & d.ToString("F4"))
Debug.WriteLine("F5 " & d.ToString("F5"))
Debug.WriteLine("F6 " & d.ToString("F6"))
Debug.WriteLine("F7 " & d.ToString("F7"))
Debug.WriteLine("G " & d.ToString("G"))
Debug.WriteLine("G12 " & d.ToString("G12"))
Debug.WriteLine("N " & d.ToString("N"))
Debug.WriteLine("N3 " & d.ToString("N3"))
Debug.WriteLine("P " & d.ToString("P"))
Debug.WriteLine("R " & d.ToString("R"))
'Debug.WriteLine("X " & d.ToString("X")) ' 無効

って感じで実行してみると、
C \1,234,567,890
D 1234567890
D12 001234567890
E 1.234568E+009
F 1234567890.00
F4 1234567890.0000
G 1234567890
G4 1.235E+09
G12 1234567890
N 1,234,567,890.00
N0 1,234,567,890
N12 1,234,567,890.000000000000
12N 12N
P 123,456,789,000.00%
X 499602D2
X8 499602D2

C \1,234,567,890
E 1.234568E+009
F 1234567890.10
F4 1234567890.0988
F5 1234567890.09877
F6 1234567890.098770
F7 1234567890.0987700
G 1234567890.09877
G12 1234567890.1
N 1,234,567,890.10
N3 1,234,567,890.099
P 123,456,789,009.88%
R 1234567890.0987654

こんな結果に。お手軽に使えるN0辺りはお勧めかも。

2007年1月27日土曜日

[VB.NET] Select Caseで色々と。

Caseの部分は結構色々書けるんですね。
複数項目の場合(1 or 2 or 4)は
Case 1,2,4
範囲指定の場合(10以上15以下)は
Case 10 To 15
またそのあわせ技
Case 1,2,4, 10 To 15
比較演算子も使えたり(100以上)は
Case Is >= 100

以上から
1か2か4か10から15の間か100以上であるなんて場合は

Select Case selectValue
 Case 1 ,2 ,4 ,10 To 15 ,Is >= 100
  ' 処理
 Case Else
  ' 例外処理
End Select

って書き方が出来るらしいです。
If文よりもすっきりです。

[VB.NET] MAPIを使ってメールの作成

画面にMAPISessionとMAPIMessagesを貼り付けて。。。

-----------------------

Me.AxMAPISession1.SignOn()

'セッションの関連付け
Me.AxMAPIMessages1.SessionID = Me.AxMAPISession1.SessionID

With Me.AxMAPIMessages1
  'メッセージを作成
  .Compose()

  'あて先
  .RecipDisplayName = "[DisplayName]"
  'メールアドレス
  .RecipAddress = "[mailaddress]"

  'Subject
  .MsgSubject = "サブジェクト"
  '本文
  .MsgNoteText = "本文"
  '添付ファイル(複数)
  For i As Int32 = 0 To filelist.Count - 1
    .AttachmentIndex = i
    .AttachmentPathName = DirectCast(filelist.Item(i), FileInfo).FullName
    .AttachmentName = "Flie" & (i + 1).ToString
    'コレが重要。スペースが必要。
    'NOTE .MsgNoteText = " " -> KB173853 in microsoft
    .MsgNoteText = " "
  Next

  Try
    .Send(True)
  Catch ex As System.Runtime.InteropServices.COMException
    Debug.WriteLine("メール送信時のキャンセルでComExceptionが発生っぽい")
    Debug.WriteLine(ex)
  End Try
End With

Me.AxMAPISession1.SignOff()

2007年1月26日金曜日

[VB.NET] 現在のログインユーザが管理者権限かをチェックする

現在のログインユーザが管理者権限かをチェックするには。。。

Imports System.Security.Principal

Private Function IsAdministrator() As Boolean

  ' 現在の Windows ユーザを表す WindowsIndentity オブジェクトを取得
  Dim Identity As WindowsIdentity = indowsIdentity.GetCurrent()
  ' ロールを評価するための WindowsPrincipal オブジェクトを
  ' WindowsIdentity オブジェクトから作成します
  Dim principal As New WindowsPrincipal(Identity)

  If principal.IsInRole(WindowsBuiltInRole.Administrator) Then
    Return True
  Else
    Return False
  End If
End Function

[APS.NET] ASP.NET 1.1が実行されてないエラーの対処

以下のサイトからの引用です。
http://www.masahiko.info/it/archives/000408.html

Visual Studio .NETでASP.NET WebアプリケーションやASP.NET Webサービスのプロジェクトを作成するとき、「指定されたWebサーバーでASP.NET Version 1.1が実行されていません。ASP.NET Webアプリケーションまたはサービスを実行することはできなくなります。」というエラーが発生する時の対処方法。

(1).NET Framework 1.1がインストールされているかチェック。インストールされていなければ、Windows Updateもしくは次のサイトから再配布可能パッケージを入手してインストールします。

(2).NET Framework 1.1がインストールされているにもかかわらず、このエラーが発生する場合は、ASP.NET 1.1を再登録する必要があります。再登録の方法は次の手順で行います。
1.「ファイル名を指定して実行」を起動します(Windowsキー+「R」キー)。
2.表示されるダイアログに「%SYSTEMROOT%\Microsoft.NET\Framework」を入力して実行。
3.表示されるエクスプローラの中からVersion 1.1用のフォルダ「v1.1.????」(例:v1.1.4322)を開きます。
4.「aspnet_regiis.exe」があるのを確認してください。
5.再度「ファイル名を指定して実行」を起動して、「cmd」と入力して実行します。
6.コマンド プロンプトが起動するので、そのウィンドウ内に先ほどの「aspnet_regiis.exe」をドラッグ&ドロップしてください。
7.すると、「aspnet_regiis.exe」のフル パスが自動的に入力されます。
8.フル パス後に1つ半角スペースを空けて「/i」(インストールの意味)と入力して実行します。
9.以上で、ASP.NETへの.NET Framework 1.1のインストールが完了します。

ちなみに、「aspnet_regiis.exe」のオプションで「/u」を入力すると、アンインストールできます。

[ASP.NET] ASP.NETでPDFをStream出力

VB.NET で Webアプリケーションを作成します。
んで、参照設定で「CrystalDecisions.*」を設定。
次にプロジェクトにCrystalレポートファイルを追加します。
ファイル名(=クラス名)MDA20で保存します。

フォームにボタンを追加してクリックイベントに以下のコードを書くと
サーバ上にpdfファイルを作成せずにクライアントにダウンロードさせることが出来まっす。

あ、imports はテキトウに設定をしないとだめだす。

--------------------------

Dim Report As New ReportDocument
Dim exportOpts As New ExportOptions
Dim b As Byte()
Dim st As System.IO.Stream

'レポートをロード
Report = New Report20

'以下エクスポート処理
exportOpts = Report.ExportOptions

'PDFを指定
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat

'
' PDFのオプション設定
'
Dim pdfOption As New PdfRtfWordFormatOptions
' ページ指定
pdfOption.FirstPageNumber = 1
exportOpts.FormatOptions = pdfOption

' ↓なぞ
Dim req As ExportRequestContext = New ExportRequestContext
req.ExportInfo = exportOpts

'出力先はStream
st = Report.FormatEngine.ExportToStream(req)

ReDim b(st.Length)
' データをバイト列に変換
st.Read(b, 0, Convert.ToInt32(st.Length))

' ブラウザに出力する場合はこれ
'Response.ContentType = "Application/pdf"
'Response.BinaryWrite(b)
'Response.End()

' 保存ダイアログを表示する場合はこっち
With HttpContext.Current.Response
.ContentType = "application/pdf"
' 日本語はだめっぽい
.AppendHeader("content-disposition", "attachment; filename=Report20.pdf")
.BinaryWrite(b)
.End()
End With