الأربعاء، 15 يونيو 2011

Top 10 Productivity Tips for Microsoft Access 2010



I've been working with Access 2010 now for about 6 months and I'm finally getting more productive. I wanted to come up with 10 "Productivivity Tips" to speed you on your learning curve, but as I documented the things that I've learned, they came out more like "annoyances". Either way, I hope you find them helpful.

1) Hide/Show the Ribbon

Unless you've adopted Access 2007, you'll find the Ribbon to be a bit irritating. Menu options aren't where you left them in Access 2003. Moreover, the thing takes up so much space it infringes on your work area. You can "minimize" the ribbon quite simply by double-clicking on one of the tabs and Access remembers your selection for future sessions.

2) Get used to Right-Clicking

Maybe it's just me but the simplest things have become hard. The old menu options for going "into design mode" still exist, but if you've minimized the ribbon (see tip # 1), then it's going to take an extra click ... or two to get there. In Access 2003, one click could toggle you between Form View and Design View. Now, after showing the ribbon (first click), the default view is "Layout View" so you must click (second click) on the drop down arrow to see the Design View option (third click).

I suppose I'm just lazy but I've taken to using the Right-Click menu. You can right-click a form, report or query to expose the View menu. That makes it a consistent 2-click process. However, there's another discrete little toolbar in the lower right corner that exposes the available View Menu options. This is a "one-click" solution I can live with, once I get used to looking there for it.
Microsoft Access 2010 View Menu options
As for opening things in the Access Navigation bar at the left, the only two ways I know of is to double click the object (which opens it) or to right-click and choose the type of view you wish. Like I said, get used to right-clicking.

3) Finding a Code Window

Maybe this is just my frustration with the menu showing again but I used to open the code window for a form or report with the menu-toolbar option. It's more complicated now because if, while working on a form in design view, you end up with the ribbon on the Create tab, the option labeled "Module" does NOT open the module behind your form but, as the name suggests, creates a NEW module.
While this is logical, it requires a diligence that I'm not accustomed to. Not only do I have to look for menu options but now I have to pay attention to which menu tab is active.
To bypass this potential hazard, I now simply press Ctl+G to open the Immediate Window thereby making the VBA editing environment available. I still have to locate my form in the object browser to load that particular code module, but at least I've stopped creating blank modules which I must later delete.

4) Home, Create, Design

While we're on the subject, the three tabs that are most important to get to know are the Home, Create and Design tabs and of these, Home is the least useful. The Create tab contains all the options for making new tables, queries, forms, etc. The Design tab allows you to work with these newly created objects, turning a SELECT query into an UPDATE query and dropping controls on your form. Once you've got controls, the Home tab is where you go to apply text formatting. It's also where the usual run-time options live, such as sorting a datasheet or filtering a form.

5) Use the Quick Access Toolbar



Things like printing are also, in my opinion, too hard. What used to be one click is now three. That is, unless you add the print or print preview options to the Quick Access Toolbar. As a rule, until one gets used to the menu options, it's a good idea to overload this toolbar with things you can't easily find in the menus.

6) Try out Navigation Forms

Navigation forms provide an alternative to the Switchboard of previous Access versions. The menu option on the Create tab (shown below) will guide you through the process and it's very intuitive. In the beta, the display and positioning of buttons was glitchy but I haven't noticed a problem in production code.
Microsoft Access 2010 Navigation forms

7) Output Reports to PDF

For some time I've been using Cute-PDF to output my invoices and other reports to PDF. PDF output support is now incorporated into Access 2010 though I believe it is more easily implemented through VBA code. From the menu system, it takes about 5 clicks to get it done as can be seen in the screen shot below. This is one of those good candidates for the Quick Access toolbar, which turns it into a single click.
=> File Menu => Save and Publish => Save Object As => PDF or XPS => Save As
Output Reports to PDF

8) Just Enable All Macros

If you, like me, hate that security warning message, then just enable all macros. I know it says it's not recommended, but why? Because if you're not careful, code might execute in this file? I hope so ... that's why I wrote it. To disable the warning you need to enable macros. Find the Access Options from the File menu and navigate to the Trust Center options. Select the Enable All Macros option and save.
Enable All Macros

9) Take Advantage of Query Intellisense

Yes, Intellisense is now available while creating queries. It shows up in the Criteria row and the Update To row for an UPDATE query. Auto-complete will assist you in getting table and column names correct. For example, if you start typing your table.column name, a list pops up from which you can select. Functions are also exposed in the Intellisense auto-complete list.
For years, I've been advocating using SET and GET functions to save and recall values for filtering data. For example, a report might be launched from a form that collects an ID or text code. You store this value using a SET function. Then the query behind the report uses a GET function call to recall the value. In Access 2010, your functions are exposed with Intellisense from the criteria row, as shown in the screen shot below.


Query Intellisense
The same is true for built-in functions so regardless of whether you use my SET-GET paradigm or not, this feature may come in handy. On the other hand, you now have to be careful when typing so that Intellisense doesn't misunderstand and auto-complete something you didn't intend.

10) Look to the Experts for More Tips

As I work with Access 2010, I'm constantly on the lookout for things that will save me time. I recently learned that some old friends from the Access newsgroups, Arvin Meyer and Douglas J. Steele, have written a book titled Access Solutions: Tips, Tricks, and Secrets from Microsoft Access MVPs. The book is published by Wiley and
these authors are two of the most respected MVPs in the Access community. From the published table of contents it's clear they cover topics that apply to previous versions of Access as well as Access 2010 specific features. If you're hungry for more tips, it's the place to look.

The PC Is Dead


The rapid rise of cloud computing, coupled with a slew of increasingly powerful mobile computing devices, is killing the PC faster than analysts had predicted.

In fact, some now say, the desktop PC is essentially dead – an immobile zombie that's already irrelevant to many consumers and soon will be to businesses, too.

Yesterday's announcement of Apple's iCloud, which will store content online and synch all of a user's Apple devices wirelessly, is the latest in a series of nails being hammered into the PC coffin. Smart phones and tablets have given rise to a new consumer demand for immediate information at our fingertips, which John Quain, industry expert and longtime technology writer, said has made the desktop computer defunct.
"We don’t need PCs anymore," Quain told BusinessNewsDaily. "They are dead."

PC sales slide

The two-stage death march is evident in surprisingly dim statistics released recently.

Two of the world's largest manufactures of personal computers, HP and Dell, recently reported significant losses in sales to consumers. PC sales to individuals for quarter ending April 30 plunged 23 percent at HP and 7 percent at Dell.

PC sales to businesses have fared better. But overall, a turning point has clearly passed.

This spring, Gartner, Inc. released statistics showing PC sales overall dropped 1.1 percent compared to last year. Meanwhile, IMS Research has forecasted a whopping 213 percent growth for tablets.

While the PC has long been considered a necessity in the workplace, Quain said mobile devices are now giving businesses the chance to break from that mold.

"I see a lot of large enterprises and small businesses reconsidering the need for a desktop computer," Quain said. "The tablet computers are much cheaper, and give small and medium-size business another option so they don’t have to invest in a desktop computer."
Meanwhile laptops, considered to be PC's, are changing rapidly. Apple's Mac Air has the instant-on capability of a tablet computer, and isn't much bigger than one. Intel recently announced Ultrabook, a thin, light tablet-like laptop with a touch screen. The company thinks it'll make up half the laptop market by the end of next year.

And earlier this year, Motorola released the Atrix 4G, a smartphone that docks to a laptop-like device that's really just a screen, keyboard and giant battery. The smartphone is the brains of the operation.

Gone from campuses

Nowhere is the PC demise more stark than among the consumers of tomorrow.

At Penn State University, Director of Education Technology Services Allan Gyorke said the former student staple is now rarely spotted in dorm rooms.

"The desktop PC is dead," Gyorke said, estimating that 95 percent of students now bring a laptop or tablet media device to campus instead of using a traditional PC in their dorm room.

Those newer devices, he said, are easier to store and easier to set up.

While not ready to put the final nail in the PC's coffin, Roger Kay, an industry expert and president of Endpoint Technologies Associates, said the growing use of media tablets and smartphones is indeed pushing it out the door.
"Death might be an over-exaggeration, but the wind has been taken out of the PC’s sails," Kay said. "There is a lot of momentum going the other way."

With tablets and smartphones having as many computer capabilities as a desktop PC, Kay said it's only naturally for people to choose the handier option. "It is hugely more convenient," Kay said. "It immediately changed my lifestyle, in that it offered a type of mobile computing that wasn’t available before."

The wireless connection

The explosion of wireless networks is also linked to the PC's demise. You no longer need to be seated at a desktop computer that is plugged into the phone line next to it to access the Internet, Gyorke noted.

"If there is a wireless connection somewhere, people want to access it," Gyorke said. "That is a real drawing force."

Apple's new iCloud will help seal the desktop's fate by spurring the use of all cloud-based services, Quain said. "It is going to make everyone feel more confident in using those cloud services."

Other analysts point out that the iCloud is mostly an Apple affair, but that Google, Microsoft and others who want a foothold in the cloud are already racing in that direction, and iCloud will only heat that race up.

Evolve or die

Still, even as he acknowledged that PC alternatives like the iPad and other media tablets are slowing personal computer sales, George Shiffler, research director at Gartner, Inc., said he expects the PC to survive by evolving into something else.

"PCs are a very flexible platform," Shiffler said. “There will be something like a PC (in the future), but it won't be exactly what it is like today.”

One scenario, he suggested, is to have a further merge of the television and computer.

"I think we may see the desktop move to an all-in-one screen," Shiffler said. "Then it becomes a media center."

Gyorke said he actually sees the future of personal computers headed in the same direction as tablets, with touch screens and app centers.

"The interface will be very similar to the iPad," Gyorke said.

الثلاثاء، 14 يونيو 2011

Advanced JavaScript Techniques


There have been a number of articles published over the years that discuss best practices techniques for JavaScript. I thought I would go a little bit beyond the scope of those articles and outline a number of advanced techniques and practices that I have personally used or read about that could be invaluable in certain circumstances.
This article doesn’t necessarily cover every detail of the methods I’m describing, but provides an overview, along with code examples, of some practical JavaScript coding techniques.

1. Closures to Extend Variable Scope

Closures in JavaScript are a fairly straightforward concept, and have been discussed online in a number of in-depth articles. The fact that they are straightforward doesn’t necessarily mean they’re simple however, as seen by the extensive articles that cover the subject.
Simply put, closures allow variable scope to be extended past the common scope restrictions of functions. I like the way Jeremy Keith describes closures in his book Bulletproof Ajax:
"Think of closures as a kind of regional scope: broader than local but not as broad as global."
To create a closure, you nest a function inside of a function. That inner function has access to all variables in its parent function’s scope. This comes in handy when creating methods and properties in object oriented scripts. Here is a simple example that demonstrates the use of a closure:
function myObject() {
  this.property1 = "value1";
  this.property2 = "value2";
  var newValue = this.property1;
  this.performMethod = function() {
    myMethodValue = newValue;
    return myMethodValue;
  };
  }
  var myObjectInstance = new myObject();
  alert(myObjectInstance.performMethod());
The key portions of the script are the nested anonymous function are highlighted in green and the method call in the alert function (last line). Because the method in the alert is actually calling a nested function, that method is able to read the value of the variable called newValue, even thought that variable is not within the scope of the anonymous function, or method.
Developers use closures all the time, probably unknowingly, since a closure is created any time an anonymous function is nested inside another function and utilizes variables from the parent function’s scope. The power of the closure is revealed when that method (the inner function) is called, and values that normally wouldn’t be accessible are within "regional" scope and are thus able to be used as any other value.

See the references below for some deeper explanations of closures and their relation to scope. I also highly recommend you pick up a good advanced JavaScript book that offers a good discussion of the concepts associated with closures.

2. Object Literals to Pass Optional Arguments

Here is a handy coding tip to keep in mind when dealing with functions that can accept a large number of optional arguments. Instead of passing the large number of arguments in the conventional fashion, which could unnecessarily complicate the function, you can pass just one argument which ends up being a collection of arguments declared in an object literal.
Let’s look, first of all, at how we might do this in the typical manner, so we can see the contrast:
function showStatistics(name, team, position, average, homeruns, rbi) {
  document.write("<p><strong>Name:</strong> " + arguments[0] + "<br />");
  document.write("<strong>Team:</strong> " + arguments[1] + "<br />");

  if (typeof arguments[2] === "string") {
    document.write("<strong>Position:</strong> " + position + "<br />");
  }
  if (typeof arguments[3] === "number") {
    document.write("<strong>Batting Average:</strong> " + average + "<br />");
  }
  if (typeof arguments[4] === "number") {
    document.write("<strong>Home Runs:</strong> " + homeruns + "<br />");
  }
  if (typeof arguments[5] === "number") {
    document.write("<strong>Runs Batted In:</strong> " + rbi + "</p>");
  }
}
showStatistics("Mark Teixeira");
showStatistics("Mark Teixeira", "New York Yankees");
showStatistics("Mark Teixeira", "New York Yankees", "1st Base", .284, 32, 101); 

The function above can take up to 6 arguments. The first two arguments are mandatory, so inside the function, we don’t check for their existence. The last 4 arguments are not mandatory, so we only display their values if they exist.
We call the function 3 different times (last 3 lines), with different numbers of arguments each time. You can see that if the number of passed arguments was in the dozens, or more, the code could look a little messy, and would be harder to maintain, or read.
Now let’s look at the same code using object literals to pass the arguments:
function showStatistics(args) {
  document.write("<p><strong>Name:</strong> " + args.name + "<br />");
  document.write("<strong>Team:</strong> " + args.team + "<br />");
  if (typeof args.position === "string") {
    document.write("<strong>Position:</strong> " + args.position + "<br />");
  }
  if (typeof args.average === "number") {
    document.write("<strong>Average:</strong> " + args.average + "<br />");
  }
  if (typeof args.homeruns === "number") {
    document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />");
  }
  if (typeof args.rbi === "number") {
    document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>");
  }
}

showStatistics({
  name: "Mark Teixeira"
});

showStatistics({
  name: "Mark Teixeira",
  team: "New York Yankees"
});

showStatistics({
  name: "Mark Teixeira",
  team: "New York Yankees",
  position: "1st Base",
  average: .284,
  homeruns: 32,
  rbi: 101
});  
Technically, this second method of passing the arguments might require a little bit more code, but with a large collection of arguments, there are a few advantages.
First, the function itself is simplified because it accepts only one argument (args), which is a collection of all the values passed from the object literal (nameteam,position, etc). Plus, the actual argument values are easy to read, and can easily be understood, updated, or modified, since the correlation between the values and the argument references are more direct.
If the function required only a small number of arguments, then this method would not be necessary, and might actually have the opposite effect. So, use this technique sparingly, and only in situations where you foresee the collection of arguments being hard to maintain over time.

3. Contextual Targeting of DOM Elements

There are sometimes instances where you need to traverse the DOM and gain access to a specific element, or group of elements, but due to certain restrictions, you may not have direct access to the elements via a CSS class name or ID in the HTML code. This might be because of user-generated content produced through a rich text editor, or dynamic content pulled from a database.
Whatever the case, it’s not impossible to access those unidentified DOM elements via JavaScript. Using what I call "contextual targeting", you can gain access to, and modify, almost any element in the DOM. As long as you have a map of the general template that contains the element you want to target, you can access that element and manipulate it the same way you would an element that has a class name or ID.
Let’s create some basic HTML code that will serve as our example page:
<div id="header">
  <h1>Site Title</h1>
</div>
<div id="sidebar">
  <ul>
    <li><a href="#">Testing</a></li>
    <li><a href="#">Testing</a></li>
    <li><a href="#">Testing</a></li>
    <li><a href="#">Testing</a></li>
    <li><a href="#">Testing</a></li>
    <li><a href="#">Testing</a></li>
  </ul>
</div>
<div id="content">
  <h2>Page Title</h2>
  <p><a href="#">Lorum Ipsum link here</a>. Pellentesque habitant morbi
     tristique senectus et netus et malesuada fames ac turpis egestas.
     Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet,
     ante. Donec eu libero sit amet quam egestas semper.
     Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
     Pellentesque habitant morbi tristique senectus et netus et malesuada
     fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae,
     ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam
     egestas semper. Aenean ultricies mi vitae est. Mauris
     placerat eleifend leo.</p>
  <p><span style="color: red;">Pellentesque habitant morbi</span>
    tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum
    tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec
    eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
    Mauris placerat eleifend leo. Pellentesque habitant morbi tristique senectus
    et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,
    feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit
    amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat
    eleifend leo.</p>
</div>
<div id="footer">
   <p>Copyright | <a href="#">contact</a> | <a href="#">policy</a> |
      <a href="#">privacy</a></p>
</div>  
Using the HTML code above, if we wanted to target all the anchor tags on the page, we could collect them and manipulate them like this:
var myLinkCollection = document.getElementsByTagName("a");

for (i=0;i<myLinkCollection.length;i++) {
  // do something with the anchor tags here
}  
If we wanted to target only the anchor tags in the footer, however, we would target them based on their context, or surrounding elements, like this:
var myFooterElement = document.getElementById("footer");
var myLinksInFooter = myFooterElement.getElementsByTagName("a");
for (i=0;i<myLinksInFooter.length;i++) {
  // do something with footer anchor tags here
}  
The first line grabs a reference to the footer element. The second line collects all <a>tags inside the footer. Then we loop through them and do what we want with them. Thus, they are accessible even though they are not grouped via class names.
You can accomplish the same thing by using node properties, as shown below.
var myLinkCollection = document.getElementsByTagName("a");

for (i=0;i<myLinkCollection.length;i++) {
  if (myLinkCollection[i].parentNode.parentNode.id === "footer") {
    // do something with footer anchor tags here
  }
}  
Similar code could be used to target the lone anchor tag inside the "content" section.
We could also limit our anchor tag search to include only tags that have the hrefattribute set, so as to avoid finding any in-page links. We do this by using thegetAttribute method:
var myLinkCollection = document.getElementsByTagName("a");

for (i=0;i<myLinkCollection.length;i++) {
  if (myLinkCollection[i].getAttribute("href")) {
    // do something with the anchor tags here
  }
}  
Finally, you’ll notice that there is a <span> tag with an inline style. The inline style could have been generated through a content management system, so you may not have the ability to edit it directly. You can target all <span> elements with inline styles like this:
var myLinkCollection = document.getElementsByTagName("span");

for (i=0;i<myLinkCollection.length;i++) {
  if (myLinkCollection[i].getAttribute("style")) {
    // do something with all anchors that have inline styles
  }
}  
The possibilities are endless with contextual targeting, and there are even more options available if you’re using a JavaScript library that normalizes browser differences and simplifies DOM manipulation.

4. Using Namespaces to Prevent Conflicts

If you’re doing an extensive amount of raw JavaScript coding and suspect that additions could be made to the same pages you’re working on, you can prevent any future conflicts with your code by giving your code its own namespace.
Object-oriented JavaScript implements namespace-like principles due to the fact that properties and methods are declared inside of objects, thus there are less likely to be conflicts. A conflict could arise, however, through object names. And very likely, the conflict will occur "silently", thus you may not be alerted to the issue immediately.
You can prevent all conflicts by creating a unique namespace. Let’s use theshowStatistics function to demonstrate how we can encapsulate code into its own namespace:
if (typeof MY == "undefined") {
  MY = new Object();
  MY.CUSTOM = new Object();
}

MY.CUSTOM.namespace = function() {
  function showStatistics(args) {
    document.write("<p><strong>Name:</strong> " + args.name + "<br />");
    document.write("<strong>Team:</strong> " + args.team + "<br />");
    if (typeof args.position === "string") {
      document.write("<strong>Position:</strong> " + args.position + "<br />");
    }
    if (typeof args.average === "number") {
      document.write("<strong>Average:</strong> " + args.average + "<br />");
    }
    if (typeof args.homeruns === "number") {
      document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />");
    }
    if (typeof args.rbi === "number") {
      document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>");
    }
  }

  showStatistics({
    name: "Mark Teixeira",
    team: "New York Yankees",
    position: "1st Base",
    average: .284,
    homeruns: 32,
    rbi: 101
  });
}
MY.CUSTOM.namespace();  
The first few lines create the namespace by checking to see if the "MY" object already exists. This object can be whatever you want it to be. Just pick a name that you don’t think will ever be used again. After the MY object is created, we are then able to create the "CUSTOM" object as a property of the MY object. Then our namespacefunction becomes a method of the MY.CUSTOM object. Keep in mind that "MY", "CUSTOM" and "namespace" can each be your own custom names. I chose these for demonstration purposes. They could be CHEESEBURGER.ONIONS.pickles if you want!
The showStatistics function is exactly the same as in the example earlier that utilizes an object literal to pass in the values. But in this case, the entire function, including the object literal, is encapsulated inside my.custom.namespace. The last line invokes the entire function using dot notation, and the function runs exactly the same as it normally would, except that it is protected from conflicting with another function called "showStatistics".

5. Hybrid Application Development

You can create powerful JavaScript applications if you use a combination of a JavaScript library and raw JavaScript code. Many JavaScript libraries are used to implement "pretty" animations and other customizable effects–sometimes via plugins– that often don’t require much to be added to them other than some custom values.
On the other hand, there may be situations where you’ll want to accomplish something specificly requested by a client. Maybe it’s something not available in a library and that requires extensive coding, possibly utilizing Ajax and a variety of DOM methods.
There is no point in reinventing the wheel. You can implement your favorite JavaScript library and take advantage of its simplified Ajax calls, DOM methods, and normalization of browser differences. Thus, you can have the advantages of the library, while still creating custom scripts that are specific to your project.

6. Rendering Readable HTML

Finally, this is a technique to use in situations that require dozens of lines of HTML code being generated dynamically via JavaScript. Take the following example:
var pageContainer = document.getElementById("container");
var pageTitle = "Content Title";
var authorBio = "Mr. Lorum Ipsum";
var pageContent = "Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here.";
var footerContent = "Copyright 2009";
var HTMLCode = '\n<h1>' + pageTitle + '</h1>\n
               <div id="content">\n
               <p>' + pageContent + '</p>\n
               <div id="author_bio">\n
               <p>' + authorBio +'</p>\n
               </div>\n
               </div>\n
               <div id="footer">
               <p>' + footerContent + '</p>\n
               </div>\n';

pageContainer.innerHTML = HTMLCode;  

The line to take note of above is the one that declares the value of the HTMLCode variable. It renders just find in the generated source code, since it utilizes the "new line" character, so it looks like perfectly good HTML. But if this line of code were any longer it would be extremely difficult to read and maintain in the .js file.

Global Graphics gDoc

Here is the same code as above, but implementing a much more organized method of displaying the HTML:
var pageContainer = document.getElementById("container");
var pageTitle = "Content Title";
var authorBio = "Mr. Lorum Ipsum";
var pageContent = "Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here. Lorum ipsum line text here.
                   Lorum ipsum line text here.";
var HTMLCode =  '\n' +
                '<h1>' + pageTitle + '</h1>\n'
                '<div id="content">\n' +
                  '<p>' + pageContent + '</p>\n' +
                  '<div id="author_bio">\n' +
                    '<p>' + authorBio + '</p>\n' +
                  '</div>\n'          '</div>\n' +
                '<div id="footer">' +
                  '<p>' + footerContent + '</p>\n' +
                '</div>\n';

pageContainer.innerHTML = HTMLCode;
Now the code is much more readable, and conforms to the manner in which HTML is rendered in an actual HTML page. It even includes proper HTML indenting, and still uses the new line character to properly format the outputted HTML.

الأربعاء، 8 يونيو 2011

how to open,read and write to Files in C#


The Namespace

System.IO provides all the necessary classes, methods, and properties for manipulating directories and files. Table 1 elaborates the main classes under this namespace.

ClassPurpose/Use
Binary Reader and WriterRead and write primitive data types
Directory, File, DirectoryInfo, and FileInfoCreate, delete, and move files and directories. Get specific information about the files by making use of the properties defined in these classes.
FileStreamAccess the files in a random fashion
MemoryStreamAccess data stored in memory
StreamWriter and StreamReaderRead and write textual information
StringReader and StringWriterRead and write textual Information from a string buffer

Table 1—Classes under System.IO


Working with DirectoryInfo and FileInfo classes

The base class of DirectoryInfo and FileInfo is FileSystemInfo. This is an abstract class, meaning you can't instantiate this class. But you can use the properties defined by this class. Table 2 elaborates its properties and methods.
PropertiesPurpose/Use
AttributesReturns attributes associated with a file. Takes FileAttributes enumeration values
CreationTimeReturns the time of creation of the file
ExistsChecks whether a supplied file is a directory or not
ExtensionReturns the file extension
LastAccessTimeReturns last accessed time of the file
FullNameReturns the full path of the file
LastWriteTimeReturns the time of last written activity to the file
NameReturns the name of a given file
Delete()Deletes a file. Be careful when using this method.
Table 2—Members of FileSystemInfo class



Working with DirectoryInfo and FileInfo classes

The base class of DirectoryInfo and FileInfo is FileSystemInfo. This is an abstract class, meaning you can't instantiate this class. But you can use the properties defined by this class. Table 2 elaborates its properties and methods.
PropertiesPurpose/Use
AttributesReturns attributes associated with a file. Takes FileAttributes enumeration values
CreationTimeReturns the time of creation of the file
ExistsChecks whether a supplied file is a directory or not
ExtensionReturns the file extension
LastAccessTimeReturns last accessed time of the file
FullNameReturns the full path of the file
LastWriteTimeReturns the time of last written activity to the file
NameReturns the name of a given file
Delete()Deletes a file. Be careful when using this method.
Table 2—Members of FileSystemInfo class

The DirectoryInfo class provides methods for creating, moving, and deleting directories. To make use of the above properties, create an object of the DirectoryInfo class as shown in Listing 1:
Listing 1
DirectoryInfo dir1 = new DirectoryInfo(@"F:\WINNT");
You then can access the properties by using the object dir1, as shown in the code fragment in Listing 2:
Listing 2
Console.WriteLine("Full Name is : {0}", dir1.FullName);
Console.WriteLine("Attributes are : {0}",
                   dir1.Attributes.ToString());
You can also apply the values of FileAttributes enumeration. Its values are shown in Table 3.
PropertiesPurpose/Use
ArchiveReturns the file's Archive status
CompressedReturns whether the file is compressed or not
DirectoryReturns whether the file is a directory or not
EncryptedReturns whether the file is encrypted or not
HiddenReturns whether the file is hidden or not
OfflineSignifies that the data is not available
ReadOnlyIndicates that the file is read only
SystemIndicates that the file is a System file (probably a file under the Windows folder)
Table 3—FileAttributes Enumeration Values

Working with Files under a Directory

Suppose that you want to list all BMP files under the f:\Pictures directory. You can write a code as shown in the code snippet given in Listing 3:
Listing 3
DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
FileInfo[] bmpfiles = dir.GetFiles("*.bmp);
Console.WriteLine("Total number of bmp files", bmpfiles.Length);
Foreach( FileInfo f in bmpfiles)
{
  Console.WriteLine("Name is : {0}", f.Name);
  Console.WriteLine("Length of the file is : {0}", f.Length);
  Console.WriteLine("Creation time is : {0}", f.CreationTime);
  Console.WriteLine("Attributes of the file are : {0}",
                     f.Attributes.ToString());
}

Creating Subdirectories

You can easily create a subdirectory. Listing fragment 4 describes how to create a subdirectory called MySub under the Sub directory.
Listing 4
DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
try
{
  dir.CreateSubdirectory("Sub");
  dir.CreateSubdirectory(@"Sub\MySub");
}
catch(IOException e) 
{
  Console.WriteLine(e.Message);
}

Creating Files by Using the FileInfo Class

With the FileInfo class, you can create new files, access information about the files, delete, and move files. This class also provides methods for opening, reading from, and writing to a file. Listing 5 shows how to create a text file and access its information like its creation time, full name, and so forth.
Listing 5
FileInfo fi = new FileInfo(@"F:\Myprogram.txt");
FileStream fstr = fi.Create();
Console.WriteLine("Creation Time: {0}",f.CreationTime);
Console.WriteLine("Full Name: {0}",f.FullName);
Console.WriteLine("FileAttributes: {0}",f.Attributes.ToString());

//Way to delete Myprogram.txt file.

Console.WriteLine("Press any key to delete the file");
Console.Read();
fstr.Close();
fi.Delete();

Understanding the Open() Method

The FileInfo class defines a method named Open() with which you can create files by applying the values of the FileMode and FileAccess enumerations. The code snippet in Listing 6 describes its usage:
Listing 6
FileInfo f = new FileInfo("c:\myfile.txt");
FileStream s = f.Open(FileMode.OpenorWrite, FileAccess.Read);
You then can read from and write to a file by using the object 's'. In the overloaded Open() method, permission is given only for reading from a file. If you want to write to a file, you have to apply the ReadWrite value of FileAccess enumeration. Tables 4 and 5 describe the values of the FileMode and FileAccess enumerations.
ValuesPurpose/Use
AppendOpens the file and adds data. This should be used with the FileAccess Write Enumeration value.
CreateCreates a new file. Overwrites any existing file.
CreateNewCreates a new file. If the file already exists, IOException is thrown.
OpenOpens an existing file
OpenOrCreateOpens a new file. If there is no file, it creates a new file.
TruncateTruncates an existing file
Table 4—FileMode Enumeration values
ValuesPurpose/Use
ReadData can be read (retrieved) from the file
ReadWriteData can be added to and retrieved from the file
WriteData can be added to the file
Table 5—FileAccess Enumeration values

Writing to a Text File by Using the StreamWriter Class

You can easily write texts or other information to a file by using the CreateText() method of the FileInfo class. However, you have to obtain a valid StreamWriter. It's this StreamWriter reference that provides the required functionalities for writing to a file. To illustrate, Listing 7 writes a series of texts to the Mytext.txt file.
Listing 7
FileInfo f = new FileInfo("Mytext.txt")
StreamWriter w = f.CreateText();
w.WriteLine("This is from");
w.WriteLine("Chapter 6");
w.WriteLine("Of C# Module");
w.Write(w.NewLine);
w.WriteLine("Thanks for your time");
w.Close();

Reading from a Text File

You can read from a Text file by using the StreamReader class. For this, you have to specify the file name using the static OpenText() method of the File class. Listing 8 reads the contents that we have written in Listing 7:
Listing 8
Console.WriteLine("Reading the contents from the file");
StreamReader s = File.OpenText("Mytext.txt");
string read = null;
while ((read = s.ReadLine()) != null)
{
  Console.WriteLine(read);
}
s.Close();