Hey everyone!!
Today I will be teaching you on how to read and write from a file in C#....
First you will need a DLL called "PackageIO.dll" you can do it without it but it´s alot easier this way!
PackageIO Download : https://www.mediafire.com/?9ycfytxit437rbz
Also you might need an hex converter.... : http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html
Now on Visual Studio press Project -> Add Reference -> Browse -> Browse... and select it (the dll) then press Ok
Ok, now add :
using PackageIO; to the usings section :)
Now add a string called path under your partial class..
string path;
Alright...
Now let´s make a OpenFileDialog for our Open Button
OpenFileDialog open = new OpenFileDialog();
open.Title = "Choose your DF3 Text File..."; //it says DF3 because in the video tutorial I pretended this was for a game called DF3
open.Filter = "Text Files (.txt) | *.txt";
if (open.ShowDialog == DialogResult.OK)
{
path = open.FileName;
...
}
else
{
MessageBox.Show("Could not open file!!");
}
Now if you are just opening a regular file you can just put a sucess MessageBox in DialogResult.OK...
However as I am making for this "DF3" Game I will add prior verification.. I added the word DF3GAMEFILE on Offset 0x0..
So do this
if (open.ShowDialog() == DialogResult.OK)
{
path = open.FileName;
Reader readfile = new Reader(path);
readfile.Position = 0x0;
if (readfile.PeekString(0xB) == "DF3GAMEFILE") //0x0B is the lenght
{
MessageBox.Show("The file has been opened!");
groupBox1.Enabled = true;
....
}
else
{
MessageBox.Show("The file has been found however it was not a DF3 File!!!");
}
}
else
{
MessageBox.Show("Could not open file!!");
}
Alright now get something to read from... I will be reading the suposed MOTD.. which I putted on offset 0x225 (if it was an actual game you wouldnt put it there you would find it ;))
So to read it add this to the sucess open file dialog
readfile.Position = 0x225;
textBox1.Text = Convert.ToString(readfile.PeekString(0x3F));
Alright .. also "readfile.PeekString(0x3F)); 3F is the length... you can find the lenght on HxD..
Now let´s write to the file the contents of textBox1.Text..
first set textBox1.Text Max length to the length allowed by default on the file (till it reaches the next null byte (00)
by doing :
textBox1.MaxLength = 63;
Why 63?? ... Because thats how much 3F is converted to normal numbers :) ...
Ok now just add this to your button
Writer write = new Writer(path, Endian.Little, 0L);
write.Position = 0x225;
write.WriteString(textBox1.Text);
And your done!!!
ALSO : Don´t forget of the 0x before the offset ;)!!
Note if I forgot something let me know :)
Final Source : http://pastebin.com/zsSp8hE3
Video Version : http://youtu.be/4NmYWArhH1s
No comments:
Post a Comment