Alone In The Database

Friday, August 11, 2006

 

Just a Test of Getting Scrapbook Data Posted To Blogger

First of all - this is a capture of an article from:
 http://www.informit.com/articles/article.asp?p=482928&f1=rss&rl=1

If it works, I guess it's like stealing their content.  But that's not my intent.  I'm just testing posting from my Scrapbook to Blogger.  Please visit the original link to support the site that provided this article.






File Management in .NET

File Management in .NET


In two recent articles we looked at file access in the .NET
framework—how to write data to and read data from both binary and text
files. There’s one more aspect of file programming that a developer needs
to know about, and that’s the file management tasks:


  • Creating and deleting folders
  • Moving, copying, and deleting files
  • Getting information about drives, folders, and files

I’m glad to say that the .NET Framework’s approach to file
management is well designed and easy to use.


Working with Files


There are two .NET classes that you’ll use for copying, moving,
deleting, and getting information about files: the File class and the FileInfo
class. Both classes are part of the System.IO namespace. They differ as
follows:


  • To use the FileInfo class, you must create an instance of the class
    that’s associated with the file of interest. You then call the appropriate
    methods to perform operations on that file.
  • The File class is an abstract class and cannot be instantiated. You call the
    File class’s methods and pass as a parameter the name of the file you want
    to manipulate.

Let’s look at an example of how these two classes differ in usage.
Suppose you want to determine the date and time that a file was created.
Here’s how you would accomplish this task with FileInfo:


Dim d As DateTime
Dim f As New FileInfo("c:\mydatafile.dat")
d = f.CreationTime

In contrast, here’s how you would use the File class for the same
task:


Dim d As DateTime
d = File.GetCreationTime("c:\mydatafile.dat ")

Table 1 describes the members of the File and FileInfo classes that perform
frequently needed file-management tasks.


Table 1 Members of the File and FileInfo classes.


View Table


File management actions provide many opportunities for exceptions to be
thrown. Here are just a few examples:


  • Trying to copy a nonexistent file
  • Passing a filename/path that’s too long
  • Trying to delete a file when you don’t have the required
    permissions

I advise that you place all file management code in Try..Catch blocks to
ensure that unhandled exceptions cannot occur. I won’t go into the details
of the possible exceptions here, but you can find the details in the .NET
documentation.


Let’s compare some examples of using the File and FileInfo classes. To
move the file c:\new_data\report.doc to the folder c:\old_data under the same
filename, here’s the code using the FileInfo class:


Dim f As New FileInfo("c:\new_data\report.doc")
f.Move("c:\old_data\report.doc")

To perform the same task using the File class, you would write this:


File.Move("c:\new_data\report.doc", " c:\old_data\report.doc")

To delete the file c:\documents\sales.xls, you can use this code:


File.Delete("c:\documents\sales.xls")

Or you could use the following code:


Dim f As New FileInfo("c:\documents\sales.xls")
f.Delete

Make a New Comment

You must login in order to post a comment.






File Management in .NET - 2

File Management in .NET

Working with Drives and Directories


On a Windows computer, persistent storage—in other words, disk
drives—is organized into directories (also called
folders). Each drive has a single root directory. The root directory
may have one or more subdirectories. Those subdirectories may in turn have their
own subdirectories, and so on. Network sharing introduces additional complexity.
You can refer to shared network drives and directories by using their share name
(for example, \\MainServer\MyFolder) or a mapped drive letter. Following are
some of the tasks related to drives and directories that you may need to perform
in your .NET programs:


  • Determining what drives exist
  • Determining whether a specific directory exists
  • Creating, moving, and deleting directories
  • Setting the current directory

The .NET Framework provides two classes for performing these tasks. The
Directory class exposes static (shared) methods for working with directories,
whereas the DirectoryInfo class provides instance methods. (This is the same as
the distinction between the FileInfo and File classes.) Both classes are part of
the System.IO namespace.


Because the file system treats a directory as a special kind of file, many
class members that are available in the File and FileInfo classes are also
present in the Directory and DirectoryInfo classes. Specifically, FileInfo
members that are also present in DirectoryInfo are as follows:


  • CreationTime
  • Delete
  • LastAccessTime
  • LastWriteTime
  • MoveTo

The DirectoryInfo class constructor takes as its one argument a string that
specifies the target directory. Here’s the syntax:


New(path)

where path is a string giving the path of the directory. An
exception will be thrown if the directory doesn’t exist, if the path
string isn’t properly formed, if the user doesn’t have the required
permission, or if the path is longer than 256 characters. Here’s an
example of creating an instance of this class:


Dim d As New DirectoryInfo("c:\documents")

The Directory and File classes also have some members in common:


  • Delete
  • GetCreationTime
  • GetLastAccessTime
  • GetLastWriteTime
  • Move

Table 2 shows some additional members of the Directory class; Table 3
describes additional members of the DirectoryInfo class.


Table 2 Additional members of the Directory class.












































Class Member



Description



CreateDirectory(path)



Creates the specified directory and, if necessary, the path to it. Returns a
type DirectoryInfo for the new directory.



GetCurrentDirectory()



Returns a type String containing the path of the current working
directory.



GetDirectories(path)



Returns, in an array of type String, the names of all subdirectories in the
directory specified by path.



GetDirectoryRoot(path)



Returns the volume and root information for the specified directory (for
example, "c:\").



GetFiles(path)



Returns, in an array of type String, the names of all files in the specified
directory.



GetFileSystemEntries(path)



Returns, in an array of type String, the names of all files and
subdirectories in the specified directory.



GetLogicalDrives()



Returns, in an array of type String, all the logical drives on the system, in
the form "<drive letter>:\".



GetParent(path)



Returns a type DirectoryInfo referencing the parent of the specified
directory).



SetCurrentDirectory(path)



Sets the application’s working directory to the specified
directory.



Table 3 Additional members of the DirectoryInfo class.




























Class Member



Description



Parent



Returns a type DirectoryInfo referring to the parent directory of the
instance directory.



Root



Returns a type DirectoryInfo referring to the root directory.



CreateSubdirectory(path)



Creates the subdirectory specified by path and returns a type
DirectoryInfo referring to the new directory.



GetDirectories()



Returns an array of type DirectoryInfo containing references to all of the
subdirectories in the instance directory.



GetFiles()



Returns an array of type FileInfo containing references to all of the files
in the instance directory.



Notice that the Directory class has methods for getting and setting the
current working directory. What exactly is this?


Any running application has a working directory, which is where the
application’s file operations (such as opening a file) will occur by
default—that is, if no path is specified for the operation. The working
directory is also the location from which relative path specifications are
determined. During program development in Visual Studio, the working directory
is by default the bin directory within the project directory. After deployment,
it’s the directory where the application’s .exe file is located.
It’s rarely a good idea to rely on the default working directory, however,
so your code should either change the working directory or always specify the
complete path for file operations.


Let’s wrap up this article with some examples of using the Directory
and DirectoryInfo classes.



The following code uses the Directory class to display, in the immediate
window, a list of all subdirectories in the c:\documents directory:


Dim sa() As String
Dim s As String
sa = Directory.GetDirectories("c:\documents")
For Each s In sa
Debug.WriteLine(s)
Next

This code performs the same task, but uses the DirectoryInfo class:


Dim d As New DirectoryInfo("c:\documents")
Dim da() As DirectoryInfo
Dim x As DirectoryInfo
da = d.GetDirectories
For Each x In da
Debug.WriteLine(x.FullName)
Next

The next example checks to see whether a specified directory exists. If so, a
message to that effect is displayed. If not, the directory is created.


Const DIR_PATH = "c:\my_new_documents"
If Directory.Exists(DIR_PATH) Then
MsgBox(DIR_PATH & " already exists.")
Else
Directory.CreateDirectory(DIR_PATH)
MsgBox(DIR_PATH & " created successfully.")
End If

For a final demonstration, I’ll show you how to determine the total
number of files in a directory and all of its subdirectories, along with their
total size (see Listing 1). The program makes use of a powerful technique called
recursion, whereby a function calls itself repeatedly to carry out a
repetitive task. Here’s how it works. The function, called
FilesInDirectory(), is passed a type DirectoryInfo referencing the directory of
interest. Code in the function obtains a list of all files in that directory and
gets the count and total size of these files, adding them to a summary
(maintained in a structure that’s defined in the code). Then the code gets
a list of all subdirectories that the current directory contains, and calls
itself for each one. As a result the code "worms" its way down through
all levels of subdirectories, and gets the file information for each one.
Recursion is not used all that often, but in appropriate situations it’s a
very powerful programming technique.



Listing 1 Using recursion to list files and their sizes.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim result As FileSummary
Dim msg As String

result = FilesInDirectory(New DirectoryInfo("c:\documents"))

msg = "The directory and its subdirectories contain "
msg &= result.Count.ToString & " files totaling "
msg &= result.TotalSize.ToString & " bytes."
MsgBox(msg)

End Sub

Private Function FilesInDirectory(ByVal d As DirectoryInfo) As FileSummary

’ Returns the total number and total size of files in d
’ and all of its subdirectories.
Dim fs1 As New FileSummary()
Dim fs2 As New FileSummary()
Dim fa() As FileInfo
Dim f As FileInfo

’ Get the files in this directory.
fa = d.GetFiles
’ Add their info to the summary.
For Each f In fa
fs1.Count += 1
fs1.TotalSize += f.Length
Next

’ Now do the same for all the subdirectories.
Dim da() As DirectoryInfo
Dim d1 As DirectoryInfo
da = d.GetDirectories
For Each d1 In da
fs2 = FilesInDirectory(d1)
fs1.Count += fs2.Count
fs1.TotalSize += fs2.TotalSize
Next

’ Return the current totals.
Return fs1

End Function

Structure FileSummary
Public TotalSize As Long
Public Count As Integer
End Structure

Make a New Comment

You must login in order to post a comment.



Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

05/21/2006 - 05/28/2006   05/28/2006 - 06/04/2006   06/11/2006 - 06/18/2006   06/18/2006 - 06/25/2006   07/09/2006 - 07/16/2006   07/16/2006 - 07/23/2006   07/23/2006 - 07/30/2006   08/06/2006 - 08/13/2006   08/13/2006 - 08/20/2006   10/01/2006 - 10/08/2006   10/08/2006 - 10/15/2006   10/22/2006 - 10/29/2006   11/26/2006 - 12/03/2006   01/07/2007 - 01/14/2007   03/25/2007 - 04/01/2007   04/15/2007 - 04/22/2007   04/29/2007 - 05/06/2007   05/06/2007 - 05/13/2007   05/13/2007 - 05/20/2007   06/10/2007 - 06/17/2007   07/29/2007 - 08/05/2007   09/09/2007 - 09/16/2007   12/09/2007 - 12/16/2007   06/29/2008 - 07/06/2008   10/26/2008 - 11/02/2008   11/23/2008 - 11/30/2008   12/28/2008 - 01/04/2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]