GridViewRow MSDN
Wednesday, November 28, 2007
Tuesday, November 20, 2007
Linux: Find files on your computer with find
A standard Linux system has an incredible amount of files installed. Looking for a file location can be a painful task to do though a file browser.
Fortunately, there is a nifty command line available by default on any Linux distribution: find.
find can virtually find anything on your computer and comes with a lot of options. This tutorial will introduce a basic use of it and show how you can search your filesystem for file names matching a name pattern.
On Debian based distros, find is part of the package findutils. find allow one to search for files on a filesystem based on different condition, creation date, modified date, file size, file type, permissions, name ....
In this tutorial, I will be focused on finding files/directories based on their name, in order to explain in more depth the syntax of find, I will also show how you can narrow down your search by adding condition on size and file modification time.
This will suit most searches, if you need more details, I would recommend looking at the find's manpage.
1. Find basis
The default syntax of find is as such:
find [path] [expression]
where path is the path used as root for searching pattern and expression the expression we want the file to match.
2. Finding a file based on filename
Let say for instance you want to find all .avi files in users home directories. Search files can be found with the following command:
# find /home -name '*.avi'
If you want to search for *.mpg and *.avi files, you will use the following:
find /home -name '*.mpg' -o -name '*.avi'
Case insensitive searches can be achieved by using the -iname switch:
find /home -iname '*.mpg' -o -iname '*.avi'
3. Adding some more criterias
Those kind of searches might returns far too many results, making it hard to find waht you were looking for in the first place.
Fortunately, you can narrow down the search by adding criteria such as the file size and the file modification date.
Let'search for .avi files bigger than 700M. This can be done with:
find /home/ -name '*.avi' -a -size +700M
Now, let's find the same subset of files that were modified less than 15 days ago:
find /home/ -name '*.avi' -a -size +700M -mtime -15
4. Adding some actions
Grand, we can now find files based on a subset of criteria. What
would be even better is to apply some actions on those files. Action
can be done with the use of -exec switch.
We can now find .avi file that are newer that 15 days, in this example,
we are going to move those file to another location: /my/new/movies . I
consider that this directory already exist on your system.
Moving .avi files bigger than 700M and younger than 15 days to /my/new/movies can be done with:
find /home/ -name '*.avi' -a -size +700M -mtime -15 -exec mv '{}' /my/new/movies/ \;
Mind the use of '{}' and \; (there is a space before \;).
'{}' matches the file that was found, while \; terminate the exec statement.
5. Conclusion
find is a powerful tool with an extensive set of statement. This article only covered a small subset of available features. For more information on the find command I recommend checking out its man page.
Powered by ScribeFire.
Saturday, November 10, 2007
Single Instance Application
Making an application single instance, can be achieved by using a mutex (Mutual Exclusion Semaphore).
A Windows application loads the main form through the Application.Run( )
method. In the Main
method, create a new mutex. If a new mutex is created the application is allowed to run. If the mutex has already been created, the application cannot start. This will ensure that only one instance will be running at any time.
// Used to check if we can create a new mutex
bool newMutexCreated = false;
// The name of the mutex is to be prefixed with Local\ to make
// sure that its is created in the per-session namespace,
// not in the global namespace.
string mutexName = "Local\\" +
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Mutex mutex = null;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch(Exception ex)
{
MessageBox.Show (ex.Message+"\n\n"+ex.StackTrace+
"\n\n"+"Application Exiting...","Exception thrown");
Application.Exit ();
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if(newMutexCreated)
{
Application.Run(new AnimatedWindowForm());
}
When a new mutex is created the mutex name can be prefixed with either Global\ or Local\. Prefixing with Global\ means the mutex is effective in the global namespace.
Prefixing with Local\ means the mutex is effective in the user's session namespace only.
Windows XP and Windows 2003 allow fast user switching through Terminal Services Sessions. So if a mutex is created with a Global\ prefix, the application can have only one instance system wide. So if one user launches the application, other users cannot create a second instance in their sessions. If the mutex is not prefixed with Local\ it is effective per session only.
Placing Your C# Application in the System Tray
- To get started, open an existing C# Windows form (or create a new one).
- Open the Visual Studio Toolbox.
- Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
- Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
- Set the control's Icon property to the icon that you want to appear in the System Tray
- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
- Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
private void Form1_Resize(object sender, System.EventArgs e) { if (FormWindowState.Minimized == WindowState) Hide(); }
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) { Show(); WindowState = FormWindowState.Normal; }
At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.
Figure 1
Now, let's see the steps involved with adding a context menu to the icon.
- From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
- Right-click the ContextMenu control and select the Edit Menu.option.
- Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
- As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
- Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.