Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Tuesday, May 29, 2012

Online Compilers

Two cool online compilers for code snippets:
http://codepad.org and http://ideone.com

I tried this C++ Fibonacci function and the compiler worked:

#include <iostream>
 
int fib(int n)
{
     if ( n == 0 ) return 0;
     if ( n == 1 ) return 1;
 
     return fib(n-1) + fib(n-2);
}
 
int main(){
 printf("Fib Val: %i", fib(7));
 return 0;
}

Thursday, May 17, 2012

.NET C# Get Byte array from a file


public static byte[] GetBytesFromFile(string file)
       {
            try
            {
                // check if this is a file name;
                if (file == null)
                    return null;

                if (file.Trim().Length == 0)
                    return null;

                // get the size of the buffer
                int bufferSize = (int)(new System.IO.FileInfo(file).Length);

                // check if the file has anything.
                if (bufferSize == 0)
                    return null;

                // create the buffer with the proper size
                byte[] buffer = new byte[bufferSize];

                // create the stream
                System.IO.FileStream fs = 
new System.IO.FileStream(file, System.IO.FileMode.Open);

                // create the reader of the stream
                System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);

                // populate the buffer
                buffer = reader.ReadBytes(bufferSize);

                // Close the stream
                fs.Close();
                fs.Dispose();

                // close the reader
                reader.Close();
                fs.Dispose();

                return buffer;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Please Make sure that the file is valid or is not in use. Details:\r\n" + ex.Message);
                return null;
            }

        }

Monday, May 14, 2012

.NET C# Create a file and open it using Default Application Type


public static void OpenFileWithDefaultApplication(string fullfileLocationAndName, byte[] data)
{
  try
  {
    System.IO.FileStream fs =   new System.IO.FileStream(fullfileLocationAndName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
      fs.Write(data, 0, data.Length - 1);
      fs.Close();
      fs.Dispose();
      System.Diagnostics.Process.Start(fullfileLocationAndName);
  }
catch (Exception ex)
   {
       MessageBox.Show("There was an error opening the file: " + ex.Message);
}
}

Sunday, May 6, 2012

.NET C# Write to a file

public static bool WriteToFile(string fileName, string fileContent)
{
    fileName = fileName;
    System.IO.TextWriter tw;
    try
    {
        tw = new System.IO.StreamWriter(fileName, true);
        tw.WriteLine(fileContent);
        tw.Close();
        return true;
    }
    catch{}
    return false;
} 

.NET C# Read from a file

public static string ReadFromFile(string fileName)
{
    string ret = "";
    System.IO.StreamReader sr;
    try
    {
        sr = new System.IO.StreamReader(fileName);           
        ret = sr.ReadToEnd();
        sr.Close();
        return ret;
    }
    catch{ return null; }
}