Programming Tutorial On The Way

free online programming resource on dotnet, csharp, web related topics

In .NET, you can use the FileSystemWatcher to notify when a particular event occurs in the filesystem, such as the renaming of a particular file or directory, the increasing or decreasing of the size of a file, the user deleting a file or directory, the creation of a file or directory, or even the changing of a file or directory’s attribute(s).

The WaitForChanged method of the FileSystemWatcher class can be called to wait synchronously for an event notification. This is illustrated in the following method, which waits for an action¡ªmore specifically, the action of creating the Backup.zip file somewhere on the C:\ drive¡ªto be performed before proceeding on to the next line of code, which is the WriteLine statement. Finally, we ask the ThreadPool to use a thread to go create the file in question using the PauseAndCreateFile method, so that the FileSystemWatcher can detect the file creation: Read the rest of this entry »

Popularity: 4% [?]

Locking Subsections of a File

If a file is opened within your application and the FileShare parameter of the FileStream.Open call is set to FileShare.ReadWrite or FileShare.Write, other code in your application can alter the contents of the file while you are using it. To handle file access with more granularity, use the Lock method on the FileStream object to prevent other code from overwriting all or a portion of your file. Once you are done with the locked portion of your file, you can call the Unlock method on the FileStream object to allow other code in your application to write data to the file or that portion of the file.
Read the rest of this entry »

Popularity: 4% [?]

Say we needed to drive the CMD.EXE application to display the current time with the TIME /T command (it is possible to just run this command from the command line, but this way we can demonstrate an alternative method to drive an application that responds to standard input). The way to do this is to launch a process that is looking for input on the standard input stream. This is accomplished via the Process class StartInfo property, which is an instance of a ProcessStartInfo class. The Process.Start method will launch a new process, but the StartInfo property controls many of the details of what sort of environment that process executes in.

First, make sure that the StartInfo.RedirectStandardInput property is set to true. This setting notifies the process that it should read from standard input. Then set the StartInfo.UseShellExecute property to false because if you were to let the shell launch the process for you, it would prevent you from redirecting standard input.
Read the rest of this entry »

Popularity: 2% [?]

There is always time to use a temporary file in the application. This file will exist only as long as the process that created it remains running.
Use the static GetTempPath and GetTempFileName methods on the Path class.

The Path class provides two methods for working with temporary files. The first is the static GetTempPath method, which returns the path to the temporary directory specified in the TEMP environment variable.

The second static method, GetTempFileName, will automatically generate a temporary filename, create a zero-length file in the user’s temporary directory, and return a string containing this filename and its path.
Read the rest of this entry »

Popularity: 2% [?]

Verifying a Path

This article shows a way of verifying a path for invalid characters before it can be used in your application. The path information can be possibly entered by the user. It does not verify that the directory or path exists; use the Directory.Exists or File.Exists methods to perform this verification.

We use several of the static fields and methods in the Path class. We begin by writing a method called CheckUserEnteredPath, which accepts a string containing a path entered by the user and a bool value to decide whether we want to find all illegal characters or just the occurrence of any illegal character. Just finding any illegal character is much faster if you don’t care which illegal characters are present. This method first calls another method, either FindAnyIllegalChars or FindAllIllegalChars. If there are no illegal characters in this path, it is then checked for the existence of a file and extension. Read the rest of this entry »

Popularity: 2% [?]

When working with environment variables in particular, there are a number of cases in which several paths may be concatenated together and you need to parse each one individually. To distinguish each individual path from the others, Microsoft Windows uses the semicolon character. (Other operating systems might use a different character; Unix, Linux, and Mac OS X use a colon.) To make sure that we always use the correct path separation character, the Path class contains a public static field called PathSeparator. This field contains the character used to separate paths in the current platform. This field is marked as read-only, so it cannot be modified.

To obtain each individual path contained in a single string, use the Split instance method from the String class. This method accepts a param array of character values that are used to break apart the string instance. These individual strings containing the paths are returned in a string array. We can then simply use the foreach loop construct to iterate over each string in this string array, and we can use the various static methods of the Path class to operate on each individual path string.
Read the rest of this entry »

Popularity: 2% [?]

Parsing a Path

The Path class contains methods that can be used to parse a given path. Using these classes is much easier and less error-prone than writing path- and filename-parsing code. There are five main methods used to parse a path: GetPathRoot, GetDirectoryName, GetFileName, GetExtension, and GetFileNameWithoutExtension. Each has a single parameter, path, which represents the path to be parsed:
Read the rest of this entry »

Popularity: 3% [?]

Obtaining the Directory Tree

This article shows on how to get a directory tree, potentially including filenames, extending from any point in the directory hierarchy. In addition, each directory or file returned must be in the form of an object encapsulating that item. This will allow you to perform operations on the returned objects, such as deleting the file, renaming the file, or examining/changing its attributes. Finally, you potentially need the ability to search for a specific subset of these items based on a pattern, such as finding only files with the .pdb extension.
Read the rest of this entry »

Popularity: 4% [?]

« Previous Entries