Post: [C#]- Return File Data
05-23-2012, 02:12 AM #1
Correy
I'm the Original
(adsbygoogle = window.adsbygoogle || []).push({}); i couldn't find a function which actually does this, so i decided to create a custom one.
It's only a short code, but believe me.. it saves a lot of time and is very useful.

- What it does.
Grabs every piece of data within a file or from a position and length, then returns it to a string.

    
[COLOR="#0000FF"]public string[/COLOR] GetData([COLOR="#0000FF"]string[/COLOR] directory)
{
[COLOR="#0000FF"]using[/COLOR] ([COLOR="#40E0D0"]StreamReader[/COLOR] Reader = [COLOR="#0000FF"]new[/COLOR] [COLOR="#40E0D0"]StreamReader[/COLOR](directory))
{
[COLOR="#0000FF"]string[/COLOR] readData = Reader.ReadToEnd();
Reader.Close();
[COLOR="#0000FF"]return[/COLOR] readData;
}
}


Example of usage:
    
[COLOR="#40E0D0"]MessageBox[/COLOR].Show( GetData( [COLOR="#40E0D0"]Application[/COLOR].StartUpPath + [COLOR="#B22222"]"\\mydocument.txt"[/COLOR] );


Ouput:
any data which was in mydocument.txt.
05-23-2012, 04:03 AM #2
or...

    
public static byte[] getBytes(string directory)
{
byte[] Bytes = system.io.File.ReadAllBytes(directory);
return Bytes;
}


or if you wanted to return it as a string.

    
public static string getBytes(string directory)
{
byte[] Bytes = system.io.File.ReadAllBytes(directory);
return system.text.asciiencoding.ascii.getString(Bytes);
}


Enzo

Edit: Syntax Fixed, I was tired when I wrote lmfao.
Last edited by Jakes625 ; 05-23-2012 at 10:09 PM.

The following 3 users say thank you to Jakes625 for this useful post:

Correy, Newelly, Pichu
05-23-2012, 12:36 PM #3
fill0botto95
You talkin to me?
Originally posted by SatanicAmerican View Post
or...

    
public shared function getBytes(string directory) as byte[]
{
byte[] Bytes = system.io.file.readAllBytes(directory);
return Bytes;
}


or if you wanted to return it as a string.

    
public shared function getBytes(string directory) as string
{
byte[] Bytes = system.io.file.readAllBytes(directory);
return system.text.asciiencoding.ascii.getString(Bytes);
}


Enzo



To open text use ReadAllText method in System.IO namespace, File class
05-23-2012, 09:01 PM #4
Originally posted by fill0botto95 View Post
To open text use ReadAllText method in System.IO namespace, File class


He wanted to read all the bytes.

By "reading" a text file he meant like a stream.

ie:

byte[] header = stream.readBytes(24);
byte[] fileSize = stream.readInt32();
byte[] fileSize = stream.readLong(2);

etc.
05-23-2012, 10:58 PM #5
Pichu
RIP PICHU.
Originally posted by SatanicAmerican View Post
He wanted to read all the bytes.

By "reading" a text file he meant like a stream.

ie:

byte[] header = stream.readBytes(24);
byte[] fileSize = stream.readInt32();
byte[] fileSize = stream.readLong(2);

etc.


Just saying, when you are dealing with bytes and you don't have a lot of them, give it the name buffer as buffer deals with bytes. byte[] buffer = ?
05-24-2012, 01:57 AM #6
Originally posted by Pichu View Post
Just saying, when you are dealing with bytes and you don't have a lot of them, give it the name buffer as buffer deals with bytes. byte[] buffer = ?


Just giving a basic example.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo