Post: [C#] Creating, Reading and Starting Files in Form
06-13-2011, 10:41 PM #1
Correy
I'm the Original
(adsbygoogle = window.adsbygoogle || []).push({}); i've had a few request from this from others, not just from this forum but i figured it will come useful.
all this coding will happen inside a Form, e.g. by the click off a button.

before we start, on every Form you are inluding this in you will need to add the following..


    
using System.IO;


right, now lets get started.

[multipage=Creating A File ]
Creating A File
add this to where ever you want, on a button or what ever.
    
StreamWriter SW = new StreamWriter(@"NextGenUpdate.txt", true);

you need to include the file format, as you can see i'm using the .txt which is a text file, you can add any format but you just need to know what to put at the end.

Now here's a little Add-on which i always do, what it does if there's a file already the it will delete it, i've simplified this but you can edit it and add message boxe's to say Overwrite ? ect.
    
if (File.Exists(@"NextGenUpdate.txt"))
{
File.Delete(@"GPAD0_MP.PRF");
StreamWriter SW = new StreamWriter(@"NextGenUpdate.txt", true);
//MessageBox to show were it's been saved.
MessageBox.Show("File has Been successfully Created\n Saved To :" + Application.StartupPath)
}
else
{ StreamWriter SW = new StreamWriter(@"NextGenUpdate.txt", true); }


so, that should of Created a Text file into the Application Start Up Path

[multipage=Reading and Writing A File ]
add this Code to a button or whatever, this is a set folder.. so if you creating for a certian file this part will show you, i will be using..

TextBox : textEditorControl1
Settings : Settings.Default.SettingName
Timer : timer1

to start, add this code a the top. note, it will need editing
    
using [COLOR="red"]TestForm[/COLOR].Properties;

change the TestForm to you Program Name

this will creat the percific file and inject text into it, add it someone where in the Form
    
InjectText()
{
StreamWriter SW = new StreamWriter(@"Tutorial.txt", true);
SW.Write(Settings.Default.SettingName);
SW.Close();
}


so, that declares that it will write what ever is the Setting, the Setting will be what is in The textBox.
you will need to go to properties and add the textBox_change Code, then add this.
    
{
timer1.Start();
Settings.Default.SettingName = textEditorControl1.Text;
InjectText();
}

so, thats done.
that should now update every time you time something into that text box, no need for a save button.

[multipage=Starting Up Application or Files ]
this is a small, easy but usefull code.
it allows you to open a percific file, i do this for opening an attatchment to my program.

now, you can either do it from the application start up patch which is not in a folder or within the application start up patch inside a folder.

not inside a folder
    
System.Diagnostics.Process.Start("C://" + Application.StartupPath + "/FILENAME.FORMAT");


this code will open a File inside a folder
    
System.Diagnostics.Process.Start("C://" + Application.StartupPath + "/FOLDER/FILENAME.FORMAT");


see, thats easy right ?
Last edited by Correy ; 06-14-2011 at 12:44 AM.

The following user groaned Correy for this awful post:

Epic?
06-14-2011, 04:18 PM #2
kiwimoosical
Bounty hunter
Even easier:
    File.Delete("filepath");//delete file

byte[] buffer = File.ReadAllBytes("filepath");//reads all of the bytes in a file to a byte[]

File.WriteAllBytes("path", byte[]);//writes the byte[] to the specified post

string whatever = File.ReadAllText("path");//reads all of the text as a string into the string (char*)

/*this is how you append to a file*/

string filePath = "c:\\text.txt";
string dataToWrite = "trolololol";
File.WriteAllText(filePath, File.ReadAllText(filePath) + dataToWrite);

System.Diagnostics.Process.Start(filePath); //Open a file with default configuration

File.Move("oldFilePath", "newFilePath"); //Move a file

The following user thanked kiwimoosical for this useful post:

Correy
06-14-2011, 08:49 PM #3
Correy
I'm the Original
Originally posted by kiwimoosical View Post
Even easier:
    File.Delete("filepath");//delete file

byte[] buffer = File.ReadAllBytes("filepath");//reads all of the bytes in a file to a byte[]

File.WriteAllBytes("path", byte[]);//writes the byte[] to the specified post

string whatever = File.ReadAllText("path");//reads all of the text as a string into the string (char*)

/*this is how you append to a file*/

string filePath = "c:\\text.txt";
string dataToWrite = "trolololol";
File.WriteAllText(filePath, File.ReadAllText(filePath) + dataToWrite);

System.Diagnostics.Process.Start(filePath); //Open a file with default configuration

File.Move("oldFilePath", "newFilePath"); //Move a file


yeah nice, thanks for this..
06-15-2011, 04:45 PM #4
kiwimoosical
Bounty hunter
Originally posted by xCorrey
yeah nice, thanks for this..


No problem.
06-15-2011, 10:41 PM #5
Correy
I'm the Original
Originally posted by kiwimoosical View Post
No problem.


what types of programs do you make ?
06-16-2011, 04:31 PM #6
kiwimoosical
Bounty hunter
Originally posted by xCorrey
what types of programs do you make ?


Any and all, desktop programs, mobile phone programs, console programs, anything really.
06-16-2011, 09:01 PM #7
Correy
I'm the Original
Originally posted by kiwimoosical View Post
Any and all, desktop programs, mobile phone programs, console programs, anything really.


call of duty mod tools, anything like that ?
06-16-2011, 09:17 PM #8
fill0botto95
You talkin to me?
is there a way to build the exe?
06-17-2011, 12:54 AM #9
Epic?
Awe-Inspiring
Originally posted by fill0botto95 View Post
is there a way to build the exe?


What do you mean?

Do you mean taking the code he's provided and turning it into an executable? Or do you mean creating executables like how he creates files?
06-17-2011, 07:10 AM #10
fill0botto95
You talkin to me?
Originally posted by AsianInvasion View Post
What do you mean?

Do you mean taking the code he's provided and turning it into an executable? Or do you mean creating executables like how he creates files?


the second....in C# express 2010 i can't find this option

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo