Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, December 23, 2009

 

Uses recursion.  And System.IO

image

Had a debugging issue in the code, where there was an extra space which caused a –1 to be displayed in Directories found.

The catch block in the class was suppressing the error, so I put in a throw statement in the catch block which stopped the VS debugger, and pointed me to the correct place.

catch (Exception ex)
{
   //throw;  // great while debugging to get the line number of the exception
   return -1;
}
Error Logger

A useful class that writes to a textfile log, then can read back from it.  Creates the file if it doesn’t exist.  Appends to the file if it does exist.

image

Reading:
string pfn = Path.Combine(pathName, fileName);
            if (File.Exists(pfn))
            {
                sr = new StreamReader(pfn);
                buff = sr.ReadToEnd();
                sr.Close();
                return buff;
            }

Writing:
sw = new StreamWriter(Path.Combine(pathName, fileName), true);
                sw.WriteLine(currentDT.ToShortDateString() + ", " +
                             currentDT.ToShortTimeString() + ": " +
                             errorMessage);
                sw.WriteLine("-----------------------------");
                sw.Close();
Random Access Files

image

Uses frmMain for display logic, and clsRandomAccess for writing and reading data to disk.

In clsRandomAccess there are Property Methods for all fields:

public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            if (value.Length > 0)                   // Do we have a string?
            {
                firstName = value;
                if (firstName.Length > NAMESIZES)   // Too long
                {
                    firstName = firstName.Substring(0, NAMESIZES);  // Trim it.
                }
            }
        }
    }

All methods return a 0 if error, otherwise 1 if success.  Uses a binarywriter, and has to calculate the size of each record to know where to read to.

 

Serialize and Deserialize

image

Uses [Serialize] attribute on clsSerial class.

/// <summary>
 /// To serialize the contents of the class
 /// </summary>
 /// <param name="serial"></param>
 /// <returns>0 on error, 1 otherwise</returns>
 public int SerializeFriend(clsSerial myFriend)
 {
     try
     {
         BinaryFormatter format = new BinaryFormatter();
         FileStream myStream = new FileStream("Test.bin", FileMode.Create);
         format.Serialize(myStream, myFriend);
         myStream.Close();
     }
     catch (Exception ex)
     {
         string buff = ex.Message;
         return 0;
     }
     return 1;
 }
Multiple Document Interface

image

All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview