By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,630 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,630 IT Pros & Developers. It's quick & easy.

help with recursice *.* createfile open

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.