for information on extensions: click here
These are some things that should usually be in an extension.
1. Imports
VB.NET
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text;
These imports are always useful to have in any console application
Messages are always important to have in your program. Whether it's an error message or just an information message, they help the user know what's going on.
The first thing that should pop up on the user's screen is a message saying what the extension is. Here's an example from my wikipedia extension:
VB.NET
Console.WriteLine("command line files wikipedia extension 0.1")
C#
Console.WriteLine("command line files wikipedia extension 0.1");
All this does is let the user know that the program has started and what it is. The next type of message is the success message. Here's an example from CLF:
VB.NET
Console.WriteLine("directory '" + DirectoryToCreate + "' created")
C#
Console.WriteLine("directory '" + DirectoryToCreate + "'created");
These let the user know that everything went fine. If you don't have success messages, it's hard to tell what's happening.
And lastly we have the error message. These let the user know if something went wrong, obviously. Example:
VB.NET
Console.WriteLine("failed to create folder")
C#
Console.WriteLine("failed to create folder");
Once again, if you don't have these, the user won't realise something went wrong until they need to use the folder they tried to create in the example. So, if there's a chance to put a useful message in, put it in.
If your program has any complexity at all, errors will happen. The best way to stop your program from crashing is to use try-catch. There are two types of errors, errors you create and errors that happen by chance. Now, if there's an error you created, you don't want to use try-catch. You'll want to fix this error. If it's an error that happened by chance, then try-catch is very useful. Here is an example from my wikipedia extension:
VB.NET
Try Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp + "\clf") Catch ex As Exception Console.WriteLine("failed to create temporary folder") End Try
C#
try { Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp + "\\clf"); } catch (Exception ex) { Console.WriteLine("failed to create temporary folder"); }
This is a simple way to prevent your program from crashing or displaying an ugly error message. Along with showing simple messages, you can actually also display the actual error that was returned. Here's an example:
VB.NET
Try Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp + "\clf") Console.WriteLine("directory '" + DirectoryToCreate + "' created") Catch ex As Exception Console.WriteLine("error creating directory: " + ex.Message.ToLower) End Try
C#
try { Directory.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp + "\\clf"); Console.WriteLine("directory '" + DirectoryToCreate + "' created"); } catch (Exception ex) { Console.WriteLine("error creating directory: " + ex.Message.ToLower); }
Using this method, not only do you let the user know that something went wrong, you tell them what happened. The most important places to use try-catch is when dealing with file and folder IO and when downloading files. Obviously there are a lot of other places where try-catch is needed though. Be on the watch for areas where you might get errors.
This is just a small section but you should nonetheless read it. When you download or create a file, you shouldn't leave it on the system unless it's needed. If it's just a file that needs to be read by the program once, you should delete it when your program is done using it. An example can be found in my wikipedia extension:
VB.NET
My.Computer.Network.DownloadFile(Url, My.Computer.FileSystem.SpecialDirectories.Temp + "\clf-data\wikipedia_" + ArticleToSearch + ".html") Dim SR As New StreamReader(My.Computer.FileSystem.SpecialDirectories.Temp + "\clf-data\wikipedia_" + ArticleToSearch + ".html") Dim Result As String = SR.ReadToEnd SR.Close() File.Delete(My.Computer.FileSystem.SpecialDirectories.Temp + "\clf-data\wikipedia_" + ArticleToSearch + ".html")
C#
My.Computer.Network.DownloadFile(Url, My.Computer.FileSystem.SpecialDirectories.Temp + "\\clf-data\\wikipedia_" + ArticleToSearch + ".html"); StreamReader SR = new StreamReader(My.Computer.FileSystem.SpecialDirectories.Temp + "\\clf-data\\wikipedia_" + ArticleToSearch + ".html"); string Result = SR.ReadToEnd; SR.Close(); File.Delete(My.Computer.FileSystem.SpecialDirectories.Temp + "\\clf-data\\wikipedia_" + ArticleToSearch + ".html");
In the wikipedia extension, the program downloads the file, reads it, then deletes it. First of all, this deletes unnecessary files and it prevents errors if you try to save to that same file name.
If you follow all of these tips, your extension should improve greatly.
As you know, the only extensions that are installable are the ones I have approved and added to the extension list. So, here how do you test your extension, using CLF's Alias functionality:
Here's a useful snippet for checking if a url exists:
VB.NET
Private Function CheckUrl(ByVal Url As String) Dim UrlToCheck As New System.Uri(Url) Dim req As System.Net.WebRequest Dim UrlIsValid = False req = System.Net.WebRequest.Create(UrlToCheck) Dim resp As System.Net.WebResponse Try resp = req.GetResponse() resp.Close() req = Nothing UrlIsValid = True Catch ex As Exception req = Nothing Return False UrlIsValid = False End Try Return UrlIsValid End Function
C#
private object CheckUrl(string Url) { System.Uri UrlToCheck = new System.Uri(Url); System.Net.WebRequest req = null; dynamic UrlIsValid = false; req = System.Net.WebRequest.Create(UrlToCheck); System.Net.WebResponse resp = null; try { resp = req.GetResponse(); resp.Close(); req = null; UrlIsValid = true; } catch (Exception ex) { req = null; return false; UrlIsValid = false; } return UrlIsValid; }
I use this if I'm downloading a file. It makes it easier to report errors with the url.
When you're done working on your extension, and you think your ready for submission, email me at jordanfitz@users.sourceforge.net to request a developer code.