Click here to Skip to main content
Click here to Skip to main content

Magnetic Tape Data Storage. Part 1: Tape Drive - IO Commands

, 12 Sep 2006
Rate this:
Please Sign up or sign in to vote.
This article describes the simple way to implement Read/Write operation on tape device


This article describes the simple way to implement Read/Write operation on tape device. Attached zip file contains TapeOperator class that exposes Load, Read, Write and Close methods and BlockSize property. All these can be used as part of backup utility.

Step 1 - Configuration

Congratulations, you've bought a tape device and connected it to your PC. Now you can implement simple backup utility in C#. Actually there are three ways to work with your tape:

  1. You can operate it via driver
  2. You can work with it via some management software (if it's provided by the device manufacturer)
  3. And the last, simplest way to do it is to operate it via OS handle. This is our case; I'll show to you how you can operate it via OS handle

It is well known that each external device is interpreted by OS as a file, it's true for tape devices as well. The only thing that you have to find out before we start is the appropriate device file name. Open the device manager Window of your PC and you'll see your tape device under the Tape drives node (see picture 1 below).

Sample screenshot

Picture 1

Perform mouse right click on your tape device node and choose Tape Symbolic Name tab. In this tab you can see tape symbolic name, Tape0 in our example (see Picture 2). This name can be used to create a handle for your device.

Sample screenshot

Picture 2

So the appropriate file name for your device is @file://./Tape0.

Step 2 - Implementation

TapeOperator methods description:

  1. Load: gets as parameter name of your tape device ( "\\.\Tape0"). It creates a handle to the device by CreateFile and performs PrepareTape which executes certain procedures to prepare the given tape for I/O, both methods are part of Win32 API and therefore INTEROP is used to invoke it. Now you have the OS handle for your tape device, and it can be interpreted like a regular file handle, I mean you can open System.IO.FileStream. The only difference is that you can't seek opened FileStream, instead use SetTapePosition Win32 API (see read and write operations).
    /// <summary>
    /// Loads tape with given name. 
    /// </summary>
    public void Load( string tapeName )
        // Try to open the file.
        m_handleValue = CreateFile(
        if ( m_handleValue.IsInvalid )
            throw new TapeOperatorWin32Exception(
                "CreateFile", Marshal.GetLastWin32Error() );
        // Load the tape
        int result = PrepareTape(
        if ( result != NO_ERROR )
            throw new TapeOperatorWin32Exception(
                 "PrepareTape", Marshal.GetLastWin32Error() );
        m_stream = new FileStream(
  2. Write: gets the start position and byte array to write. As it was shown, now you can access your tape device via the opened FileStream. Just invoke FileStrem.Write and FileSream.Flush methods. One thing you have to remember, all I/O operations on tape must be done in multiplies of block size. Each device has min, default and max block sizes (in my example I've used default block size - 65536), these values can be fetched by GetTapeParameters method (Win32 API), for more information, see BlockSize property implementation.
    /// <summary>
    /// Writes to the tape given stream starting from given position
    /// </summary>
    /// <param name="startPos"></param>
    /// <param name="stream"></param>
    public void Write( long startPos, byte[] stream )
        // Get number of blocks that will be needed to perform write
        uint numberOfBlocks = GetBlocksNumber( stream.Length );
        // Updates tape's current position
        SetTapePosition( startPos );
        byte[] arrayToWrite = new byte[ numberOfBlocks * BlockSize ];
        Array.Copy( stream, arrayToWrite, stream.Length );
        // Write data to the device
        m_stream.Write( stream, 0, stream.Length );
  3. Read: gets the start positions and number of bytes to read.

    *By the way, pay attention on SetTapePosition (Win32 API) last parameter, it's type is BOOL in WIN32 definition. Don't pass .NET Boolean type for BOOL and BOOLEAN of WIN32. The size of .NET Boolean is 2 bytes, the size of BOOL is 4 bytes and size of BOOLEAN is 1 byte, therefore each Win32 API method with BOOL or BO<code>OLEAN parameter will return error for .NET Boolean pass attempt.

    /// <summary>
    /// Read one logical block from tape 
    /// starting on the given position
    /// </summary>
    /// <returns></returns>
    public byte[] Read( long startPosition )
        byte[] buffer = new byte[ BlockSize ];
        SetTapePosition( startPosition );
        m_stream.Read( buffer, 0, buffer.Length );
        return buffer;
  4. Close: closes the stream and releases unmanaged resources. You can add to this code call to LoadTape with TAPE_UNLOAD parameter to eject tape from drive.
    /// <summary>
    /// Closes handler of the current tape
    /// </summary>
    public void Close()
        if ( m_handleValue != null &&
            !m_handleValue.IsInvalid &&
            !m_handleValue.IsClosed )
  5. BlockSize: This property returns the tape's default block size by invocation of GetTapeParameters method. One of the GetTapeParameters is a reference to the structure that is filled by Win32 method. So Marshal class is used to allocate unmanaged memory and copy between managed and unmanaged structures. See the source code below:
    /// <summary>
    /// Returns default block size for current
    /// device
    /// </summary>
    public uint BlockSize
            IntPtr ptr = IntPtr.Zero;
                if ( !m_driveInfo.HasValue )
                    m_driveInfo = new DriveInfo();
                    // Allocate unmanaged memory
                    int size = Marshal.SizeOf( m_driveInfo );
                    ptr = Marshal.AllocHGlobal( size );
                    int result = 0;
                    if ( ( result = GetTapeParameters(
                        ref size,
                        ptr ) ) != NO_ERROR )
                        throw new TapeOperatorWin32Exception(
                            Marshal.GetLastWin32Error() );    
                    // Get managed media Info
                    m_driveInfo = ( DriveInfo )
                ( ptr, typeof( DriveInfo ) );
                return m_driveInfo.Value.DefaultBlockSize;
                if ( ptr != IntPtr.Zero )
                    Marshal.FreeHGlobal( ptr );
  6. See attached files for more information about defined constants, private variables, private methods and PINVOKE declarations.
  7. Good luck!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dima Statz
Israel Israel
Name: Statz Dima
Fields of interest: software

Comments and Discussions

QuestionTape Designated position read and write? Pinmemberlsyfg14-Jan-13 2:09 
QuestionTape Designated position read and write? Pinmemberlsyfg7-Jan-13 19:48 
QuestionRead data error(Have more data can be read) Pinmemberlsyfg19-Nov-12 13:59 
Questionresult GetTapeMediaParameters and read/ write problem [modified] Pinmemberseyed ali ehbali20-May-12 2:18 
QuestionHelp in read, write and exchange method Pinmemberseyed ali ehbali19-May-12 22:59 
QuestionSynchronous read exceptions Pinmemberjo3162-Aug-11 8:24 
AnswerRe: Synchronous read exceptions Pinmemberbilll papan20-Jan-12 12:47 
GeneralMicrosoft Tape Format Pinmemberrafaelverisys26-Apr-11 8:26 
QuestionWrite to blank tape Pinmemberkk wan15-Dec-10 16:25 
AnswerRe: Write to blank tape PinmemberDima Statsenko16-Dec-10 1:23 
GeneralRe: Write to blank tape Pinmemberkk wan16-Dec-10 17:50 
GeneralRe: Write to blank tape PinmemberDima Statsenko19-Dec-10 0:09 
GeneralSlow with large file Pinmemberkk wan12-Dec-10 14:55 
GeneralRe: Slow with large file PinmemberDima Statsenko15-Dec-10 2:52 
QuestionIO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations. PinmemberBlakey66627-Aug-10 1:44 
AnswerRe: IO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations. PinmemberDima_sta26-Sep-10 3:01 
GeneralQuestion About Write Operation in Code Pinmemberozbear17-Jan-10 19:59 
GeneralRe: Question About Write Operation in Code PinmemberDima_sta26-Sep-10 3:02 
GeneralRegarding tapedrive read/write operation Pinmemberprabhakarsangisetty5-Jul-09 23:56 
GeneralTape Barcode PinmemberVenu Tam12-Jun-09 3:31 
GeneralRe: Tape Barcode PinmemberDima_sta26-Sep-10 3:13 
QuestionAny news on the synchronous / asynchronous front? Pinmemberhausschuh21-Feb-09 2:50 
AnswerRe: Any news on the synchronous / asynchronous front? PinmemberDima_sta26-Sep-10 3:14 
General"When accessing a new tape of a multivolume partition, the current block size is incorrect" Pinmemberjmbillings20-Oct-08 22:50 
GeneralRe: "When accessing a new tape of a multivolume partition, the current block size is incorrect" PinmemberDima_sta26-Sep-10 3:41 
QuestionTape Programming Project Pinmembervioplex30-Sep-07 14:49 
GeneralUSB TAPE Pinmemberalberto2346-Sep-07 4:40 
GeneralRe: USB TAPE PinmemberDima_sta26-Sep-10 3:34 
QuestionWhat about VBScript ? Pinmemberstephane.reiniche9-Mar-07 0:00 
AnswerRe: What about VBScript ? PinmemberDima_sta10-Mar-07 23:38 
QuestionTape operation Visual Studio 2005 PinmemberASO12315-Jan-07 7:50 
AnswerRe: Tape operation Visual Studio 2005 PinmemberDima_sta26-Sep-10 3:39 
GeneralConfused Pinmemberreflex@codeproject12-Sep-06 12:48 
GeneralRe: Confused PinmemberDima_sta12-Sep-06 19:19 
Questionsame problem also in .Net 2.2 Pinmembermstfnoor10-Sep-06 21:24 
AnswerRe: same problem also in .Net 2.2 PinmemberDima_sta12-Sep-06 11:04 
AnswerRe: same problem also in .Net 2.2 PinmemberDima_sta18-Sep-06 22:35 
GeneralThank you. But... Pinmembermstfnoor10-Sep-06 4:43 
GeneralRe: Thank you. But... PinmemberDima_sta10-Sep-06 19:44 
GeneralRe: Thank you. But... Pinmemberlammatron20005-Jul-07 0:32 
GeneralRe: Thank you. But... PinmemberDima_sta26-Sep-10 3:21 
QuestionIs there for C# 2003 ? Pinmembermstfnoor6-Sep-06 22:01 
AnswerRe: Is there for C# 2003 ? PinmemberDima2007-Sep-06 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 12 Sep 2006
Article Copyright 2006 by Dima Statz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid