Monthly Archives: August 2011

HOW TO: Read and Write To A File In C#

Hello everyone,

Occasionally I like to review the basics to make sure I am still on track and update myself if anything has changed. Today I will be reviewing how to read and write to a file in C#. There are two methods, the easy way and the standard way. The standard way looks a little something like this.

Reading contents from a file:

Writing contents to a file:

FileStream flsFileStream = File.Open("data.tmp", FileMode.Open, FileAccess.ReadWrite);
StreamWriter srwWriter = new StreamWriter(flsFileStream);
srwWriter.WriteLine("New line to text file.");

If you are looking for less code, this is the easy way to read from a file:

string text = File.ReadAllText(@"C:words.txt");

And this is an easy way to write to a file:

File.WriteAllText(@"C:words.txt", "File contents");

Grabbing Click Events In Droid App

Hello everyone,

Over the past few weeks I have stepped into the deep end of a market that I felt was up and coming. I decided to write a droid application fresh as a java guy, because I got my new Droid 3. First off, if you have a droid you know why I did this and if you don’t have it, go try one, they are awesome! To start, I had to get thrown into a world of XML oriented UI design which I realized how much I loved from XAML. I ran through a few droid tutorials on the developer site for droid and got myself setup with Eclipse and the droid addins.

My first application, I created a simple timer that had a progress button and updated a progress bar. The idea here was to recreate a classic Progress Quest application that you may or may not know. The nice thing that I found about this whole process was how open the community was and how easy it was to pick up a lot of the concepts. Here is some sample code below that I came up with for getting a hold onto a button click event and some multi threading in Java.

public class DroidQuestActivity extends Activity implements OnClickListener {
    private ToggleButton mApplicationToggleButton;
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupFormVariables();
    }
    public void setupFormVariables() {
       mApplicationToggleButton = (ToggleButton)findViewById(R.id.ToggleAppButton);
       mApplicationToggleButton.setOnClickListener(this);    }
    }        
    public void onClick(View v) {
       // Do some click events from the button   
   }
}