473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help with recursice *.* createfile open

red
I have this:

using System;
using System.Runtime.InteropServices;
using System.Text;

class FileReader
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
IntPtr handle;

[DllImport("kernel32", SetLastError=true)]
static extern unsafe IntPtr CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);

[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(
IntPtr hFile, // handle to file
void* pBuffer, // data buffer
int NumberOfBytesToRead, // number of bytes to read
int* pNumberOfBytesRead, // number of bytes read
int Overlapped // overlapped buffer
);

[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool CloseHandle(
IntPtr hObject // handle to object
);

public bool Open(string FileName)
{
// open the existing file for reading
handle = CreateFile(
FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
0,
0);

if (handle != IntPtr.Zero)
return true;
else
return false;
}

public unsafe int Read(byte[] buffer, int index, int count)
{
int n = 0;
fixed (byte* p = buffer)
{
if (!ReadFile(handle, p + index, count, &n, 0))
return 0;
}
return n;
}

public bool Close()
{
// close file handle
return CloseHandle(handle);
}
}

class Test
{
public static int Main(string[] args)
{
//if (args.Length != 1)
if (args.Length != 1 || args[0].Equals("-help"))
{
Console.WriteLine("Usage");
return 1;
}

if (! System.IO.File.Exists(args[0]))
{
Console.WriteLine("File " + args[0] + " not found.");
return 1;
}

byte[] buffer = new byte[128];
FileReader fr = new FileReader();

if (fr.Open(args[0]))
{

// Assume that an ASCII file is being read
ASCIIEncoding Encoding = new ASCIIEncoding();

int bytesRead;
do
{
bytesRead = fr.Read(buffer, 0, buffer.Length);
string content = Encoding.GetString(buffer,0,bytesRead);

}
while ( bytesRead > 0);

fr.Close();
return 0;
}
else
{
Console.WriteLine("Failed to open requested file");
return 1;
}
}
}
How do I modify it to open aLL files i.e.:

app c:\

which opens all files in this dir

thanks

red

Nov 17 '05 #1
3 1836
Hi,

What r u doing ?

why all the p/invoke? why not use FileStream/StreamReader instead?

beside, I bet you all the files in c:\ are not text (you are assuming so by
using ASCII )

You can iterate in the files of a dir using Directory.GetFiles
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"red" <ca*****@yahoo.co.uk> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have this:

using System;
using System.Runtime.InteropServices;
using System.Text;

class FileReader
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
IntPtr handle;

[DllImport("kernel32", SetLastError=true)]
static extern unsafe IntPtr CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);

[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool ReadFile(
IntPtr hFile, // handle to file
void* pBuffer, // data buffer
int NumberOfBytesToRead, // number of bytes to read
int* pNumberOfBytesRead, // number of bytes read
int Overlapped // overlapped buffer
);

[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool CloseHandle(
IntPtr hObject // handle to object
);

public bool Open(string FileName)
{
// open the existing file for reading
handle = CreateFile(
FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
0,
0);

if (handle != IntPtr.Zero)
return true;
else
return false;
}

public unsafe int Read(byte[] buffer, int index, int count)
{
int n = 0;
fixed (byte* p = buffer)
{
if (!ReadFile(handle, p + index, count, &n, 0))
return 0;
}
return n;
}

public bool Close()
{
// close file handle
return CloseHandle(handle);
}
}

class Test
{
public static int Main(string[] args)
{
//if (args.Length != 1)
if (args.Length != 1 || args[0].Equals("-help"))
{
Console.WriteLine("Usage");
return 1;
}

if (! System.IO.File.Exists(args[0]))
{
Console.WriteLine("File " + args[0] + " not found.");
return 1;
}

byte[] buffer = new byte[128];
FileReader fr = new FileReader();

if (fr.Open(args[0]))
{

// Assume that an ASCII file is being read
ASCIIEncoding Encoding = new ASCIIEncoding();

int bytesRead;
do
{
bytesRead = fr.Read(buffer, 0, buffer.Length);
string content = Encoding.GetString(buffer,0,bytesRead);

}
while ( bytesRead > 0);

fr.Close();
return 0;
}
else
{
Console.WriteLine("Failed to open requested file");
return 1;
}
}
}
How do I modify it to open aLL files i.e.:

app c:\

which opens all files in this dir

thanks

red

Nov 17 '05 #2
red
I just need to open all files in a directory of any type, good point
about the ascii

cheers

red

Nov 17 '05 #3
Hi,

Why the P/Invoke then?

Either use FileStream or StreamReader

use Directory.GetFiles to iterate int he files of a dir

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"red" <ca*****@yahoo.co.uk> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I just need to open all files in a directory of any type, good point
about the ascii

cheers

red

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dim | last post by:
Copied directly from exaple book but not working.... All i get is an empty 0 byte file Call to GetLastError directly after the call to WriteFile returns 0 (NO_ERROR) but no data sees to be written...
4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
12
by: JLW | last post by:
I cannot get this to work correctly: File.Create("\\.\PHYSICALDRIVE0") or File.Create("\\.\Tape0") I've been searching for the better part of a week for this one. Thanks,
5
by: Andrew Clark | last post by:
Hello, Thanks for all replies on this subject. I still cannot get CreateFile to retun a good value though. I went to PInvoke.net and saw the VB.NET declaration of this function: ...
12
by: Terry Olsen | last post by:
I'm trying to create a disk image of a floppy disk. Since I can't open the device using the system.io methods, i'm trying to use the CreateFile API to get a handle for me. But the call fails...
3
by: Scott Bell | last post by:
Hello all, What is the equivalent in the .NET Framework for accessing a device at the block/sector level such as one would when using CreateFile on a device like "\\.\D:"? Attempting to create...
2
by: Jim Flanagan | last post by:
Hi... I am using VB.net Express to experiment with the Win32 API functions that are available. The current project is an application that will read the raw sectors of a logical drive so that a...
5
by: =?Utf-8?B?R2FyeQ==?= | last post by:
Been struggling with the code below for the last couple of days: m_hSerialHandle = CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED*/0, NULL); ...
0
by: private.anders | last post by:
Hi David! Really need assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.