Google


ADBRITE ads links
You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Filesystem I O

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

C# Cookbook, 2nd Edition



Problem: You need to create a new file, copy an existing file, move an existing file, or delete a file. Solution: The System.IO namespace contains two classes to perform these actions: the File and FileInfo classes. The File class contains onl...



Problem: You need to display or manipulate a file's attributes or timestamps. Solution: To display a file's timestamps, you can use either the static methods of the File class or the instance properties of the FileInfo class. The static method...



Problem: You need to rename a file. Solution: With all of the bells and whistles hanging off the .NET Framework, you would figure that renaming a file is easy. Unfortunately, there is no specific rename method that can be used to rename a file...

read more: Renaming a File


Problem: You need to determine whether a file exists prior to creating or performing an action on that file. Solution: Use the static Exists method of the File class to determine whether a file currently exists: if (File.Exists(@"c:\delete...



Problem: When you are first learning the .NET Frameworkand even for some time afterthe proper way to read to, write from, or otherwise interact with files can be unclear because the framework provides so many different ways of attacking this problem...



Problem: When reading a file, you sometimes need to move from the current position in a file to a position some number of characters before or after the current position, including to the beginning or the end of a file. After moving to this point, y...



Problem: Your application will run on more than one platform. Each platform uses a different end-of-line (EOL) character. You want your code to output the correct EOL character without having to write code to handle the EOL character specially for e...



Problem: You need to create a filepossibly for logging information to or for storing temporary informationand then write information to it. You also need to be able to read the information that you wrote to this file. Solution: To create, writ...



Problem: You need to determine whether a directory exists prior to creating or performing an action on that directory. Solution: Use the static Exists method on the Directory class to determine whether a directory currently exists: if (Dir...



Problem: You need to create a new directory, move an existing directory, or delete a directory. Solution: The System.IO namespace contains two classes to perform these actions: the Directory and DirectoryInfo classes. The Directory class conta...



Problem: You need to display or manipulate a directory's attributes or timestamps. Solution: To display a directory's timestamps, you can use either the set of static methods from the Directory object or the set of instance properties from the...



Problem: You need to rename a directory. Solution: Unfortunately, there is no specific rename method that can be used to rename a directory. However, you can use the instance MoveTo method of the DirectoryInfo class or the static Move method o...



Problem: You are attempting to find one or more specific files or directories that might or might not exist within the current filesystem. The search might need to use wildcard characters in order to widen the search, for example, searching for all ...



Problem: You need 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 y...



Problem: You need to separate the constituent parts of a path and place them into separate variables. Solution: Use the static methods of the Path class: public static void ParsePath(string path) { string root = Path.GetPathRoot(pa...

read more: Parsing a Path


Problem: You need to parse multiple paths contained in environment variables, such as PATH or Include. Solution: You can use the Path.PathSeparator field or the ; character to extract individual paths from an environment variable with a value ...



Problem: You have a pathpossibly entered by the userand you need to verify that it has no illegal characters and that a filename and extension have been provided. Solution: We use several of the static fields and methods in the Path class. We ...

read more: Verifying a Path


Problem: You need a temporary file in which to store information. This file will exist only as long as the process that created it remains running. Solution: Use the static GetTempPath and GetTempFileName methods on the Path class. To create t...



Problem: When interoperating with unmanaged code, you encounter a situation in which you are provided a file handle and no other information. This file handle must be used to open its corresponding file. Solution: In order to use an unmanaged ...



Problem: Any output that is written to one file must also be written to at least one other file. Essentially, you want to end up with at least the original file and a duplicate file. Solution: Create a class called MultiWriter with the ability...



Problem: You have an application that you need to automate and that takes input only from the standard input stream. You need to drive this application via the commands it will take over the standard input stream. Solution: Say we needed to dr...



Problem: You need to read or write data from or to a section of a file, and you want to make sure that no other processes or threads can access, modify, or delete the file until you have finished with it. Solution: Locking out other processes ...



Problem: You want to be notified when a file and/or directory is created, modified, or deleted. In addition, you might need to be notified of any of these actions for a group of files and/or directories. This can aid in alerting your application whe...



Problem: You need to be notified when a particular event occurs in the filesystem, such as the renaming of a 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 dire...



Problem: You need to programmatically compare the version information of two executable modules. An executable module is a file that contains executable code such as an .exe or .dll file. The ability to compare the version information of two executa...



Problem: Your application needs to know if a drive (HDD, CD drive, DVD drive, etc.) is available and ready to be written to and/or read from. Additionally, it would be nice to know if you have enough available free space on the drive to write inform...



Problem: You need a simple way to encrypt an existing file on the filesystem so that only the account used to encrypt the file can decrypt it. Solution: Use the Decrypt and Encrypt methods of the File class: public static void EncryptFile(...



Problem: You need a way to compress the data you write to a file using one of the stream-based classes. In addition, you need a way to decompress the data from this compressed file when you read it back in. Solution: Use the System.IO.Compress...


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Filesystem I O
   
Related tags







Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......






© CodeIdol Labs, 2007