Wrox Programmer Forums
|
BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8
This is the forum to discuss the Wrox book Professional C# 2008 by Christian Nagel, Bill Evjen, Jay Glynn, Morgan Skinner, Karli Watson; ISBN: 9780470191378
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2009, 03:21 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default reading directly from C:\

Hello
In C++ we can use fread function to read directly from C:\ drive. to do this we wrote following code
FILE *fp;
fp = fopen("C:","r+");
fread(...);

how to perform this by C#?
 
Old December 7th, 2009, 03:03 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The following code will work assuming your code has privelages to read from the C drive (this will typically mean elevating the trust level of your code or running it as administrator):

csharp Code:
using System.IO;
using System.Text;
public class Foo
{
     public static void ReadFile()
     {
         using(TextReader reader = new StreamReader(@"C:\somefile.txt"))
         {
             StringBuilder data = new StringBuilder();
             data.Append(reader.ReadToEnd());
         }
     }
}

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old December 8th, 2009, 08:22 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default

I want to read directly sectors of drive C:

for example when i want to read from c:\someText.txt directly I could use this syntax:

fread("c:\\someText.txt\\","r+");

above command reads directly from hard disk (sector by sector)
how can i perform it by C# classes?
 
Old December 8th, 2009, 09:36 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

C# does not expose any native classes that will allow you to do this so you are going to need to use P/Inovke to call functions from the Win32 API's. This is the method you will probably want to invoke: http://www.pinvoke.net/default.aspx/kernel32.ReadFile

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
The Following User Says Thank You to dparsons For This Useful Post:
irProject (December 9th, 2009)
 
Old December 9th, 2009, 12:44 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Thumbs up

Thanks a lot for your help.
 
Old December 14th, 2009, 04:19 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default

i found a sample code. that i will convert it into C#

Code:
Option Explicit


Private Type OVERLAPPED
    ternal As Long
    ternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type


Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" ( _
ByVal lpFileName As Long, _
ByVal dwDesiredAccess&, _
ByVal dwShareMode&, _
ByVal lpSecurityAttributes&, _
ByVal dwCreationDisposition&, _
ByVal dwFlagsAndAttributes&, _
ByVal hTemplateFile&) As Long

Public Declare Function ReadFile Lib "kernel32.dll" ( _
ByVal hFile As Long, _
ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByRef lpOverlapped As OVERLAPPED) As Long


Public Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject&) As Long

Public Declare Function SetFilePointer Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lDistanceToMove As Long, _
lpDistanceToMoveHigh As Long, _
ByVal dwMoveMethod As Long) As Long

Public Declare Function GetFileSize Lib "kernel32" ( _
ByVal hFile As Long, _
lpFileSizeHigh As Long) As Long
 Public Const GENERIC_READ As Long = &H80000000
 Public Const GENERIC_WRITE As Long = &H40000000

 Public Const FILE_SHARE_READ As Long = &H1&
 Public Const FILE_SHARE_WRITE As Long = &H2&

 Public Const CREATE_NEW As Long = 1&
 Public Const CREATE_ALWAYS As Long = 2&
 Public Const OPEN_EXISTING As Long = 3&
 Public Const OPEN_ALWAYS As Long = 4&
 Public Const TRUNCATE_EXISTING As Long = 5&
 Public Const FILE_BEGIN As Long = 0

Public Function OpenTextFile(FileName As String) As String

Dim FileSize As Long
Dim hFile As Long
Dim lOverLapped As OVERLAPPED
Dim nReadRetVal As Long
Dim TxtBytes() As Byte
Dim Ret As Long
Dim  I As  Integer
Dim Start As Long

 hFile = CreateFile(StrPtr(FileName), GENERIC_READ, FILE_SHARE_READ, 0&, _
    OPEN_EXISTING, 0&, 0&)


If hFile = -1 Then ' Could Be An Error
    MsgBox "Error Opening File", vbCritical, "Error"
Else

FileSize = GetFileSize(hFile, 0) 'Get The Open File Size

 ReDim TxtBytes(FileSize) As Byte

 Start = 0 'Move The File Pointer
    Call SetFilePointer(hFile, Start, 0, FILE_BEGIN) ' Read The Whole File
    
    nReadRetVal = ReadFile(hFile, TxtBytes(0), UBound(TxtBytes), Ret, lOverLapped)
     If nReadRetVal = 0 Then
        MsgBox ("Error reading from file")
    Else
   For  I = LBound(TxtBytes) To UBound(TxtBytes) 'From 0 To The Len Of the File

  OpenTextFile = OpenTextFile & Chr(TxtBytes(i))
        Next

    End  If
End  If
CloseHandle hFile

End Function
 
Old December 14th, 2009, 04:23 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default

i will send C# sample code here. I have written this sample in C# but i have not tested that does it answer to read directly from drive.
i will notice you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
just directly close the window kanoorani Javascript 1 May 5th, 2007 02:44 AM
Printing directly leo_vinay ASP.NET 1.0 and 1.1 Basics 0 October 26th, 2005 11:17 PM
Directly editing Binary HiddenFire VB.NET 4 July 6th, 2005 12:14 PM
the record after x directly akhamis SQL Language 5 November 1st, 2004 03:00 AM
Printing directly to a printer Louisa VB.NET 2002/2003 Basics 4 January 16th, 2004 09:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.