Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C#

SweetCuts

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
1 May 2012CPOL3 min read 8.9K   2   3   2
Application to open frequently used Files,Folders,Exe's,bat files and websites easily and quickly

Introduction

SweetCuts utility allows user to create links to folders, Webpage and launch file. It also allows to group them in to categories. So you can have a category called Home, Office, General etc. and each category in turn could have many links to folders/Webpage/file. Each category is a tabpage and in each tab page the links are represented as small buttons. User can click the button to initiate action.

Using the Utility

System Requirements to Run the Utility

.NET 2.0 or higher

Architecture

The application reads from config.xml and creates tabs for each category and buttons for each link/folder/file. All the updation to the XML is done using XPATH queries.

The below code reads from config.xml file and updates the list of Params structure

C#
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(m_sConfigFilePath);
XmlElement xmlRoot = xmlDoc.DocumentElement;
XmlNodeList xmlNodeList = xmlRoot.GetElementsByTagName(StringConsts.sCategories);

foreach (XmlNode xmlNode in xmlNodeList)
{
  if (xmlNode.HasChildNodes)
  {
	foreach (XmlNode xmlCategoryNode in xmlNode)
	{
	  if (xmlCategoryNode.HasChildNodes)
	  {
		foreach (XmlNode xmlChild in xmlCategoryNode)
		{
		  XmlAttributeCollection xmlCol = xmlChild.Attributes;
		  Params oParams = new Params();
		  foreach (XmlAttribute xmlAttrib in xmlCol)
		  {
			if (xmlAttrib.Name == StringConsts.sDisplayNameAttr)
			{
			  oParams.SetDisplayName(xmlAttrib.Value);
			}
			else if (xmlAttrib.Name == StringConsts.sPathAttr)
			{
			  oParams.SetLinkPath(xmlAttrib.Value);
			}
			else if (xmlAttrib.Name == StringConsts.sLaunchTypeAttr)
			{
			  oParams.SetLaunchType(xmlAttrib.Value);
			}
		  }
		  //  Add the category
		  oParams.SetCategory(xmlCategoryNode.Name);
		  m_arObjParams.Add(oParams);
		}
	  }
	}
  }
}
...

And once the content of the XML is updated to the list of Params, we create the tabpages with the buttons on it. The below code does this

C#
private bool CreateControls()
{
  try
  {
	//  Iterate through each element in the arraylist and create a button for it
	List<Params> ConfigParams = m_Config.GetConfig();

	foreach (Params oParams in ConfigParams)
	{
	  //  Create a Tab page if it is not created for the category
	  //  Else add a new button to this page if already the page is created
	  TabPage oPage = GetTabPage(oParams);
	  Point oPoint = GetNextBtnLocation(oPage);
	  Button oBtn = CreateBtn(oPoint, oParams);
	  oPage.Controls.Add(oBtn);
	}
	return true;
  }
  catch (Exception e)
  {
	//  Should write to debug window - not message box
	MessageBoxEx.Show(this,"Error in CreateControls - Description " + e.ToString());
	//  Not sure whether we can return from a catch block.
	return false;
  }
}
...

Adding new Items

After launching the SweetCuts executable, user can add links to folder, webpage or select an file by a click to Add button or by pressing Alt+A key.

  1. User will have to select from existing category or add new category from category combo box.
  2. The type of launch. Currently it could be WebBrowser(for URL)/WindowsExplorer (for folders)/Executable(any exe or other types of file which has association to executable say a txt file).
  3. Enter the Name which will be displayed on the link button. This has to be unique. It serves no purpose to have two button with the same text on it.
  4. Enter the path of the url or select the folder or file from the folder/file selection dialog.
  5. Click Add or press Alt+A key to add the item.
  6. Once done adding items click cancel or press Escape key.

Once all the validation of the input is done, the config.xml is updated with the contents and then the UI is refreshed to make the new changes visible.

The below code shows how the config.xml is updated using xpath query. All the XML parsing and updation code is stashed in to Config class. The client (here the dialog classes) creates object of Config class and calls methods on it.

C#
public bool AddItem(Params Details)
{
	if (!IsValidItem(Details.GetCategory(), Details.GetDisplayName()))
	{
	return false;
	}
	XmlDocument xmlDoc = new XmlDocument();
	xmlDoc.Load(m_sConfigFilePath);
	string xmlPath = string.Format("//{0}/{1}", StringConsts.sCategories, Details.GetCategory());
	XmlNode node = xmlDoc.SelectSingleNode(xmlPath);

	if (node == null)
	{
	// New category...
	XmlElement xNewCategory = xmlDoc.CreateElement(Details.GetCategory());
	string path = string.Format("//{0}", StringConsts.sCategories);
	XmlNode parent = xmlDoc.SelectSingleNode(path);
	node = parent.AppendChild(xNewCategory);
	//return false;
	}

	XmlElement xNewChild = xmlDoc.CreateElement(StringConsts.sItem);
	xNewChild.SetAttribute(StringConsts.sDisplayNameAttr, Details.GetDisplayName());
	xNewChild.SetAttribute(StringConsts.sPathAttr, Details.GetLinkPath());
	xNewChild.SetAttribute(StringConsts.sLaunchTypeAttr, Details.GetLaunchType());

	node.AppendChild(xNewChild);
	SaveConfig(ref xmlDoc);

	return true;
	}
}
...

Editing Existing Item

User can edit the item by Right click the link button and select the Edit option.

  1. Edit item will bring a page with existing detail. User can update the diaog and press update button or Alt+U to save changes. Once done user can press Escape key to come out of the dialog.
  2. User can delete the link by selecting Delete from the context menu.

How we update the config xml? We actually delete the element and add a new one to the xml. The below code is for deleting an item from the xml. Add item code was listed above.

C#
public bool DeleteItem(Params Details)
{
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load(m_sConfigFilePath);
  XmlElement xmlRoot = xmlDoc.DocumentElement;
  string xmlPath = string.Format("//{0}/{1}[@{2}='{3}']",Details.GetCategory(), StringConsts.sItem, StringConsts.sDisplayNameAttr, Details.GetDisplayName());
  XmlNode node = xmlRoot.SelectSingleNode(xmlPath);

  if (node == null)
  {
	return false;
  }

  node.ParentNode.RemoveChild(node);
  SaveConfig(ref xmlDoc);
  return true;
}
...

Deleting an Existing Item

User can delete an item by Right click the link button and select the Delete option. Usability
Minimize to System Tray

User can do away with the dialog once done using it by either hitting the close button or pressing the Esc key. This would keep the program alive but minimize it to the system tray with an visible pin icon.

    private void OnKeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == Convert.ToChar(Keys.Escape))
      {
        this.Hide();
        NotifyIcon.Visible = true;
      }
    }
...

User can view the path of the link (URL for webbrowser link/ Folder path for the windowsexplorer link or file path for executable link) on the tool tip which is visible when the mouse is hovered on the link button

Restore from System Tray

User can click the tray icon to restore the dialog and continue playing with it or user can press Shift + Q to bring the dialog to the front. I have used Googled code to hook key events and handle Shift+Q. I have also overloaded Show to restore the dialog from minimized position and bring the dialog to the front. The below code does exactly that.

C#
new public void Show()
{
  this.TopMost = true;
  this.Focus();
  this.BringToFront();
  this.TopMost = false;

  this.WindowState = FormWindowState.Normal;
  base.Show();
  NotifyIcon.Visible = false;
}
...

Exit the Utility

User can exit the utility by doing a click on the close button or by invoking the context menu on the tray icon and selecting exit from it.

Points of Interest

My expertise is VC++. This is my first utility using C#, so everything was a learning for me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Have been working on C++ for the past 7 years.
An amateur in C#. But given an idea can google and implement anything. Like to read and find new ways of doing things.

Comments and Discussions

 
GeneralMy vote of 1 Pin
nobodyxxxxx1-May-12 22:26
nobodyxxxxx1-May-12 22:26 
GeneralRe: My vote of 1 Pin
Vinay Kumar Shah18-Aug-12 5:28
Vinay Kumar Shah18-Aug-12 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.