[an error occurred while processing this directive] [an error occurred while processing this directive] (cache).NET TIPSFƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹‚ð“ǂݏ‘‚«‚·‚é‚ɂ́HmC#AVBn - —IT
[an error occurred while processing this directive]
y—‹LŽ–Ží•Ê—z
[an error occurred while processing this directive]
ƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹‚ð“ǂݏ‘‚«‚·‚é‚ɂ́HmC#AVBn

.NET TIPS

ƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹‚ð“ǂݏ‘‚«‚·‚é‚ɂ́HmC#AVBn

‰““¡ FM
2007/11/08
ƒfƒWƒ^ƒ‹ƒAƒhƒoƒ“ƒe[ƒW

@ƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹iƒeƒLƒXƒgEƒtƒ@ƒCƒ‹ˆÈŠO‚̃tƒ@ƒCƒ‹j‚ð“ǂݏ‘‚«‚·‚é‚ɂ́AFileStreamƒNƒ‰ƒXiSystem.IO–¼‘O‹óŠÔj‚ð—˜—p‚·‚éB

@FileStreamƒNƒ‰ƒX‚̓tƒ@ƒCƒ‹—p‚̃XƒgƒŠ[ƒ€‚ðƒTƒ|[ƒg‚·‚éƒNƒ‰ƒX‚Å‚ ‚èAStreamƒNƒ‰ƒXiSystem.IO–¼‘O‹óŠÔj‚Ì”h¶ƒNƒ‰ƒX‚Ì1‚‚¾*BŠî–{“I‚ɂ́AƒRƒ“ƒXƒgƒ‰ƒNƒ^‚ÅŽw’肵‚½ƒtƒ@ƒCƒ‹‚̃XƒgƒŠ[ƒ€‚ɑ΂µ‚āASeekƒƒ\ƒbƒh‚É‚æ‚è“ǂݏ‘‚«‚ðs‚¤ˆÊ’uŒˆ‚ß‚ðs‚¢AReadƒƒ\ƒbƒh^Writeƒƒ\ƒbƒh‚œǂݏ‘‚«‚ðs‚¤B

  • *@StreamƒNƒ‰ƒX‚Ì”h¶ƒNƒ‰ƒX‚ɂ́A‚Ù‚©‚ɁA
    • NetworkStreamƒNƒ‰ƒXiSystem.Net.Sockets–¼‘O‹óŠÔj
    • MemoryStreamƒNƒ‰ƒXiSystem.IO–¼‘O‹óŠÔj
    • BufferedStreamƒNƒ‰ƒXiSystem.IO–¼‘O‹óŠÔj
    • GZipStreamƒNƒ‰ƒXiSystem.IO.Compression–¼‘O‹óŠÔj
    • CryptoStreamƒNƒ‰ƒXiSystem.Security.Cryptography–¼‘O‹óŠÔj
  • ‚È‚Ç‚ª‚ ‚éB

Readƒƒ\ƒbƒh‚ÆWriteƒƒ\ƒbƒh

@FileStreamƒNƒ‰ƒX‚ÌReadƒƒ\ƒbƒh‚ł́Aƒpƒ‰ƒ[ƒ^‚Æ‚µ‚āA“ǂݍž‚ñ‚¾ƒf[ƒ^‚ðŠi”[‚·‚邽‚ß‚Ìbyte”z—ñA“ǂݍž‚ñ‚¾ƒf[ƒ^‚ð‘‚«ž‚ށi”z—ñ“à‚́jˆÊ’uA“ǂݍž‚ÞƒoƒCƒg”‚ðŽw’è‚·‚éB

@ƒtƒ@ƒCƒ‹‚̂ǂ̈ʒu‚̃f[ƒ^‚ð“ǂݍž‚Þ‚©‚ÍReadƒƒ\ƒbƒh‚Å‚ÍŽw’肵‚È‚¢B“ǂݍž‚Ý‚ÌŠJŽnˆÊ’u‚ÍFileStreamƒIƒuƒWƒFƒNƒg‚É‚æ‚èƒXƒgƒŠ[ƒ€‚́uŒ»ÝˆÊ’uv‚Æ‚µ‚ÄŠÇ—‚³‚ê‚Ä‚¨‚èA‚±‚ê‚ÍReadƒƒ\ƒbƒh‚ðŒÄ‚яo‚·‚½‚тɁA“ǂݍž‚ñ‚¾•ª‚¾‚¯æ‚ɐi‚ށB‚‚܂èAŒJ‚è•Ô‚µReadƒƒ\ƒbƒh‚ðŒÄ‚яo‚·‚±‚Æ‚É‚æ‚èA‡ŽŸƒf[ƒ^‚ð“ǂݍž‚ñ‚Å‚¢‚¯‚é‚Æ‚¢‚¤‚킯‚¾B

@ƒtƒ@ƒCƒ‹‚Ì”CˆÓ‚̈ʒu‚̃f[ƒ^‚ð“ǂݍž‚ނɂ́A‚ ‚ç‚©‚¶‚ßSeekƒƒ\ƒbƒh‚É‚æ‚èƒXƒgƒŠ[ƒ€‚ÌŒ»ÝˆÊ’u‚ðˆÚ“®‚³‚¹‚éBSeekƒƒ\ƒbƒh‚Ì‘æ1ƒpƒ‰ƒ[ƒ^‚ł́AŒ»ÝˆÊ’u‚ðŽ¦‚·ƒIƒtƒZƒbƒg‚ðlongŒ^‚Ì’l‚ÅŽw’è‚·‚éB‘æ2ƒpƒ‰ƒ[ƒ^‚ł́A‚»‚ꂪæ“ª‚ ‚é‚¢‚Í––”ö‚©‚ç‚̃IƒtƒZƒbƒg‚Å‚ ‚é‚©AŒ»ÝˆÊ’u‚©‚ç‚̃IƒtƒZƒbƒg‚Å‚ ‚é‚©‚ðSeekOrigin—ñ‹“‘́iSystem.IO–¼‘O‹óŠÔj‚Ì’l‚É‚æ‚èŽw’è‚·‚éB

@Readƒƒ\ƒbƒh‚Ì–ß‚è’l‚́AŽÀÛ‚ɓǂݍž‚ñ‚¾ƒoƒCƒg”‚Æ‚È‚éBƒtƒ@ƒCƒ‹‚Ì––”ö•t‹ß‚ł́A‚±‚Ì’l‚Í‘æ3ƒpƒ‰ƒ[ƒ^‚ÅŽw’肵‚½’li“ǂݍž‚ÞƒoƒCƒg”j‚æ‚è‚à¬‚³‚­‚È‚é‰Â”\«‚ª‚ ‚éB‚Ü‚½AƒXƒgƒŠ[ƒ€‚ÌŒ»ÝˆÊ’u‚ªƒtƒ@ƒCƒ‹‚Ì––”ö‚É’B‚µ‚Ä‚¢‚éê‡‚ɂ́AReadƒƒ\ƒbƒh‚Ì–ß‚è’l‚Í0‚Æ‚È‚éB

@ŽŸ‚̃R[ƒh‚́AƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹utest.binv‚ðƒI[ƒvƒ“‚µAbyte”z—ñ‚É1024bytes‚¸‚“ǂݍž‚ÞƒTƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€‚Å‚ ‚éBFileStreamƒNƒ‰ƒX‚ł́ALengthƒvƒƒpƒeƒB‚É‚æ‚èƒI[ƒvƒ“‚µ‚Ä‚¢‚éƒtƒ@ƒCƒ‹‚̃TƒCƒY‚ðŽæ“¾‚Å‚«‚éB

C#
// binread.cs

using System;
using System.IO;

class BinaryRead {
  static void Main(string[] args) {

    FileStream fs = new FileStream(
      @"test.bin", FileMode.Open, FileAccess.Read);

    int fileSize = (int)fs.Length; // ƒtƒ@ƒCƒ‹‚̃TƒCƒY
    byte[] buf = new byte[fileSize]; // ƒf[ƒ^Ši”[—p”z—ñ

    int readSize; // Readƒƒ\ƒbƒh‚œǂݍž‚ñ‚¾ƒoƒCƒg”
    int remain = fileSize; // “ǂݍž‚ނׂ«Žc‚è‚̃oƒCƒg”
    int bufPos = 0; // ƒf[ƒ^Ši”[—p”z—ñ“à‚̒ljÁˆÊ’u

    while (remain > 0) {

      // 1024Bytes‚¸‚“ǂݍž‚Þ
      readSize = fs.Read(buf, bufPos, Math.Min(1024, remain));

      bufPos += readSize;
      remain -= readSize;
    }
    fs.Dispose();
  }
}

// ƒRƒ“ƒpƒCƒ‹•û–@Fcsc binread.cs
Readƒƒ\ƒbƒh‚É‚æ‚èƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹‚ð“ǂݍž‚ÞC#‚̃Tƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€ibinread.csj

binread.cs‚̃_ƒEƒ“ƒ[ƒh

Visual Basic
' binread.vb

Imports System
Imports System.IO

Class BinaryRead
  Shared Sub Main(ByVal args As String())
    Dim fs As New FileStream( _
      "test.bin", FileMode.Open, FileAccess.Read)

    Dim fileSize As Integer = CInt(fs.Length) ' ƒtƒ@ƒCƒ‹‚̃TƒCƒY
    Dim buf(fileSize - 1) As Byte ' ƒf[ƒ^Ši”[—p”z—ñ

    Dim readSize As Integer ' Readƒƒ\ƒbƒh‚œǂݍž‚ñ‚¾ƒoƒCƒg”
    Dim remain As Integer = fileSize ' “ǂݍž‚ނׂ«Žc‚è‚̃oƒCƒg”
    Dim bufPos As Integer = 0 ' ƒf[ƒ^Ši”[—p”z—ñ“à‚̒ljÁˆÊ’u

    While remain > 0
      ' 1024Bytes‚¸‚“ǂݍž‚Þ
      readSize = fs.Read(buf, bufPos, Math.Min(1024, remain))

      bufPos += readSize
      remain -= readSize
    End While
  End Sub
End Class

' ƒRƒ“ƒpƒCƒ‹•û–@Fvbc binread.vb
Readƒƒ\ƒbƒh‚É‚æ‚èƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹‚ð“ǂݍž‚ÞVB‚̃Tƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€ibinread.vbj

binread.vb‚̃_ƒEƒ“ƒ[ƒh

@‚±‚±‚Å‚Í‹Lq—á‚ðŽ¦‚·‚½‚ß‚É1024bytes‚¸‚ƒtƒ@ƒCƒ‹‚ð“ǂݍž‚ñ‚Å‚¢‚邪Ai“ǂݍž‚Þƒtƒ@ƒCƒ‹‚̃TƒCƒY‚ª‹‘å‚Å‚È‚¯‚ê‚΁jŽŸ‚̂悤‚É‚µ‚Ä‘S‘Ì‚ðˆê“x‚ɓǂݍž‚ñ‚Å‚à‚æ‚¢B

Text only
readSize = fs.Read(buf, 0, fs.Length)

@ˆê•ûAƒoƒCƒiƒŠEƒf[ƒ^ibyte”z—ñj‚ðƒtƒ@ƒCƒ‹‚ɏ‘‚«ž‚ނɂ́AWriteƒƒ\ƒbƒh‚ðŽg—p‚·‚éBWriteƒƒ\ƒbƒh‚̃pƒ‰ƒ[ƒ^‚ɂ́Aƒf[ƒ^‚ªŠi”[‚³‚ê‚Ä‚¢‚ébyte”z—ñA‘‚«ž‚Þƒf[ƒ^‚Ì”z—ñ“à‚ł̈ʒuA‘‚«ž‚ÞƒoƒCƒg”‚ðŽw’è‚·‚éBReadƒƒ\ƒbƒh‚Æ“¯—l‚ɁA‘‚«ž‚ވʒu‚̓XƒgƒŠ[ƒ€‚ÌŒ»ÝˆÊ’u‚Æ‚È‚éiWriteƒƒ\ƒbƒh‚Əãq‚µ‚½Seekƒƒ\ƒbƒh‚̃R[ƒh—á‚ɂ‚¢‚ẮAŽŸ‚̃Tƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€‚ðŽQÆjB

“ǂݍž‚݃Gƒ‰[‚𖳎‹‚µ‚ăRƒs[‚ðs‚¤ƒTƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€

@ŽŸ‚̃R[ƒh‚́AReadAWriteASeekƒƒ\ƒbƒh‚ðŽg—p‚µ‚½ƒTƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€‚Å‚ ‚éB‚±‚̃vƒƒOƒ‰ƒ€‚ł́A“ǂݍž‚ÝŽž‚̃Gƒ‰[‚𖳎‹‚µ‚ăRƒs[‚ðs‚¤i“Ç‚ÝŽæ‚ê‚È‚©‚Á‚½•”•ª‚Í0‚Å–„‚ß‚éjB

@‚Ì•t‚¢‚½DVD‚È‚Ç‚ð“Ç‚à‚¤‚Æ‚µ‚½‚çACRCƒGƒ‰[‚ªo‚Ä‚µ‚Ü‚Á‚½‚ªAˆê•”Œ‡‘¹‚µ‚Ä‚¢‚Ä‚à‚æ‚¢‚̂ŁA‚Æ‚É‚©‚­ƒtƒ@ƒCƒ‹‚ðƒRƒs[‚µ‚½‚¢‚Æ‚¢‚Á‚½ê‡‚É—˜—p‚Å‚«‚éƒvƒƒOƒ‰ƒ€‚¾B

C#
// bincopy.cs

using System;
using System.IO;

class BinaryCopy {
  static void Main(string[] args) {

    string srcName = args[0];  // ƒRƒs[Œ³‚̃tƒ@ƒCƒ‹–¼
    string destName = args[1]; // ƒRƒs[æ‚̃tƒ@ƒCƒ‹–¼

    const int BUFSIZE = 2048; // 1“x‚ɏˆ—‚·‚éƒTƒCƒY
    byte[] buf = new byte[BUFSIZE]; // “ǂݍž‚Ý—pƒoƒbƒtƒ@
    byte[] ZEROARRAY = new byte[BUFSIZE]; // 0–„‚ß—p

    int readSize; // Readƒƒ\ƒbƒh‚œǂݍž‚ñ‚¾ƒoƒCƒg”

    using (FileStream src = new FileStream(
        srcName, FileMode.Open, FileAccess.Read))
    using (FileStream dest = new FileStream(
        destName, FileMode.Create, FileAccess.Write)) {

      while (true) {
        try {
          readSize = src.Read(buf, 0, BUFSIZE); // “ǂݍž‚Ý
        } catch {
          // “ǂݍž‚Ý‚ÉŽ¸”s‚µ‚½ê‡
          Console.WriteLine("read error at " + src.Position);

          if (src.Length - src.Position < BUFSIZE) {
            readSize = (int)(src.Length - src.Position);
          } else {
            readSize = BUFSIZE;
          }
          src.Seek(readSize, SeekOrigin.Current);
          dest.Write(ZEROARRAY, 0, readSize); // 0–„‚߂ŏ‘‚«ž‚Ý
          continue;
        }
        if (readSize == 0) {
          break; // ƒRƒs[Š®—¹
        }
        dest.Write(buf, 0, readSize); // ‘‚«ž‚Ý
      }
    }
  }
}

// ƒRƒ“ƒpƒCƒ‹•û–@Fcsc bincopy.cs
“ǂݍž‚݃Gƒ‰[‚𖳎‹‚µ‚ăRƒs[‚ðs‚¤C#‚̃Tƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€ibincopy.csj

bincopy.cs‚̃_ƒEƒ“ƒ[ƒh

Visual Basic
' bincopy.vb

Imports System
Imports System.IO

Class BinaryCopy
  Shared Sub main(ByVal args As String())

    Dim srcName As String = args(0) ' ƒRƒs[Œ³‚̃tƒ@ƒCƒ‹–¼
    Dim destName As String = args(1) ' ƒRƒs[æ‚̃tƒ@ƒCƒ‹–¼

    Dim BUFSIZE As Integer = 2048 ' 1“x‚ɏˆ—‚·‚éƒTƒCƒY
    Dim buf(BUFSIZE) As Byte ' “ǂݍž‚Ý—pƒoƒbƒtƒ@
    Dim ZEROARRAY(BUFSIZE) As Byte ' 0–„‚ß—p

    Dim readSize As Integer ' Readƒƒ\ƒbƒh‚œǂݍž‚ñ‚¾ƒoƒCƒg”

    Using src As New FileStream( _
        srcName, FileMode.Open, FileAccess.Read)
      Using dest As New FileStream( _
          destName, FileMode.Create, FileAccess.Write)

        While True
          Try
            readSize = src.Read(buf, 0, BUFSIZE) ' “ǂݍž‚Ý
          Catch
            ' “ǂݍž‚Ý‚ÉŽ¸”s‚µ‚½ê‡
            Console.WriteLine("read error at " & src.Position)

            If src.Length - src.Position < BUFSIZE Then
              readSize = CLng(src.Length - src.Position)
            Else
              readSize = BUFSIZE
            End If

            src.Seek(readSize, SeekOrigin.Current)
            dest.Write(ZEROARRAY, 0, readSize) ' 0–„‚߂ŏ‘‚«ž‚Ý

            Continue While
          End Try

          If readSize = 0 Then
            Exit While ' ƒRƒs[Š®—¹
          End If

          dest.Write(buf, 0, readSize) ' ‘‚«ž‚Ý

        End While
      End Using
    End Using
  End Sub
End Class

' ƒRƒ“ƒpƒCƒ‹•û–@Fvbc bincopy.vb
“ǂݍž‚݃Gƒ‰[‚𖳎‹‚µ‚ăRƒs[‚ðs‚¤VB‚̃Tƒ“ƒvƒ‹EƒvƒƒOƒ‰ƒ€ibincopy.vbj

bincopy.vb‚̃_ƒEƒ“ƒ[ƒh

@ƒvƒƒOƒ‰ƒ€‚ł́AReadƒƒ\ƒbƒh‚ªŽ¸”s‚µ‚½‚Æ‚«‚ɂ́i‚±‚̏ꍇ‚É‚Í—áŠO‚ª”­¶‚·‚éjA“ǂݍž‚Ý—p‚̃XƒgƒŠ[ƒ€‚ðSeekƒƒ\ƒbƒh‚É‚æ‚èi‚߁A‘‚«ž‚Ý—p‚̃XƒgƒŠ[ƒ€‚É‚Í0‚ð‘‚«ž‚ށBFileStreamƒNƒ‰ƒX‚ÌPositionƒvƒƒpƒeƒB‚̓XƒgƒŠ[ƒ€‚ÌŒ»ÝˆÊ’u‚ð•\‚·B

@‚È‚¨ASeekƒƒ\ƒbƒh‚ÍFileStreamƒIƒuƒWƒFƒNƒg‚ª“à•”‚ÅŠÇ—‚·‚錻ÝˆÊ’u‚ðˆÚ“®‚·‚邾‚¯‚ŁAƒtƒ@ƒCƒ‹‚̓ǂݍž‚ß‚È‚¢•”•ª‚ÉŒ»ÝˆÊ’u‚ðˆÚ“®‚µ‚½‚Æ‚µ‚Ä‚àŒÄ‚яo‚µ‚ÍŽ¸”s‚µ‚È‚¢BŽÀÛ‚ɃGƒ‰[‚ª”­¶‚·‚é‚̂́A‚»‚ÌŒã‚ÉReadƒƒ\ƒbƒh‚ðŒÄ‚яo‚µ‚½ê‡‚Å‚ ‚éB

—˜—p‰Â”\ƒo[ƒWƒ‡ƒ“F.NET Framework 2.0‚Ì‚Ý
ƒJƒeƒSƒŠFƒNƒ‰ƒXEƒ‰ƒCƒuƒ‰ƒŠ@ˆ—‘ΏہFƒoƒCƒiƒŠEƒtƒ@ƒCƒ‹
Žg—pƒ‰ƒCƒuƒ‰ƒŠFFileStreamƒNƒ‰ƒXiSystem.IO–¼‘O‹óŠÔj
Žg—pƒ‰ƒCƒuƒ‰ƒŠFSeekOrigin—ñ‹“‘́iSystem.IO–¼‘O‹óŠÔj
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]