System.IO.DirectoryInfo Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories.
C# Syntax:
[Serializable]
public sealed class DirectoryInfo : FileSystemInfo
Remarks
All methods of the Directory class are static and can therefore be called without having an instance of a directory. The DirectoryInfo class contains only instance methods. The static methods of the Directory class do a security check on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of DirectoryInfo instead, because the security check will not always be necessary.

Note In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all of the following are acceptable paths:

By default, full read/write access to new directories is granted to all users.

See also:
System.IO Namespace | File | FileSystemInfo.Attributes | Directory | Path

System.IO.DirectoryInfo Member List:

Public Constructors
ctor #1 Initializes a new instance of the DirectoryInfo class on the specified path.
Public Properties
Attributes
(inherited from System.IO.FileSystemInfo)
Read-write

See base class member description: System.IO.FileSystemInfo.Attributes


Gets or sets the FileAttributes of the current FileSystemInfo.
CreationTime
(inherited from System.IO.FileSystemInfo)
Read-write

See base class member description: System.IO.FileSystemInfo.CreationTime


Gets or sets the creation time of the current FileSystemInfo object.
Exists Read-only

Overridden:
Gets a value indicating whether the directory exists.
Extension
(inherited from System.IO.FileSystemInfo)
Read-only

See base class member description: System.IO.FileSystemInfo.Extension


Gets the string representing the extension part of the file.
FullName
(inherited from System.IO.FileSystemInfo)
Read-only

See base class member description: System.IO.FileSystemInfo.FullName


Gets the full path of the directory or file.
LastAccessTime
(inherited from System.IO.FileSystemInfo)
Read-write

See base class member description: System.IO.FileSystemInfo.LastAccessTime


Gets or sets the time the current file or directory was last accessed.
LastWriteTime
(inherited from System.IO.FileSystemInfo)
Read-write

See base class member description: System.IO.FileSystemInfo.LastWriteTime


Gets or sets the time when the current file or directory was last written to.
Name Read-only

Overridden:
Gets the name of this DirectoryInfo instance.
Parent Read-only

Gets the parent directory of a specified subdirectory.
Root Read-only

Gets the root portion of a path.
Public Methods
Create Creates a directory.
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
CreateSubdirectory Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.
Delete Overloaded:
Delete()

Overridden:
Deletes this DirectoryInfo if it is empty.
Delete Overloaded:
Delete(bool recursive)

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetDirectories Overloaded:
GetDirectories()

Returns the subdirectories of the current directory.
GetDirectories Overloaded:
GetDirectories(string searchPattern)

Returns an array of directories in the current DirectoryInfo matching the given search criteria.
GetFiles Overloaded:
GetFiles()

Returns a file list from the current directory.
GetFiles Overloaded:
GetFiles(string searchPattern)

Returns a file list from the current directory matching the given searchPattern, such as "*.txt".
GetFileSystemInfos Overloaded:
GetFileSystemInfos()

Returns an array of strongly typed FileSystemInfo entries listing all the files and directories.
GetFileSystemInfos Overloaded:
GetFileSystemInfos(string searchPattern)

Retrieves an array of strongly typed FileSystemInfo objects matching the specified search criteria.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
MoveTo Moves a DirectoryInfo instance and its contents to a new path.
Refresh
(inherited from System.IO.FileSystemInfo)
See base class member description: System.IO.FileSystemInfo.Refresh


Refreshes the state of the object.
ToString Overridden:
Returns the original path that was passed by the user.
Protected Fields
FullPath
(inherited from System.IO.FileSystemInfo)
See base class member description: System.IO.FileSystemInfo.FullPath


Represents the fully qualified path of the directory or file.
OriginalPath
(inherited from System.IO.FileSystemInfo)
See base class member description: System.IO.FileSystemInfo.OriginalPath


The path originally specified by the user, whether relative or absolute.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IO.DirectoryInfo Member Details

ctor #1
Summary
Initializes a new instance of the DirectoryInfo class on the specified path.
C# Syntax:
public DirectoryInfo(
   string path
);
Parameters:

path

A string specifying the path on which to create the DirectoryInfo.

Exceptions
Exception Type Condition
ArgumentNullException path is null.
DirectoryNotFoundException The path is not found.
SecurityException The caller does not have the required permission.
ArgumentException path contains invalid characters such as ", <, >, or |.
PathTooLongException The specified path, file name, or both are too long. After full qualification, each must contain fewer than 256 characters.
Remarks
The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.
.NET Framework Security:
FileIOPermission for reading from files and directories. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Field: FullPath
Inherited
See base class member description: System.IO.FileSystemInfo.FullPath

Summary
Represents the fully qualified path of the directory or file.
C# Syntax:
protected string FullPath;
Remarks


Notes to inheritors: Classes derived from FileSystemInfo can use the FullPath field to determine the full path of the object being manipulated.

Return to top


Field: OriginalPath
Inherited
See base class member description: System.IO.FileSystemInfo.OriginalPath

Summary
The path originally specified by the user, whether relative or absolute.
C# Syntax:
protected string OriginalPath;

Return to top


Property: Attributes (read-write)
Inherited
See base class member description: System.IO.FileSystemInfo.Attributes

Summary
Gets or sets the FileAttributes of the current FileSystemInfo.
C# Syntax:
public FileAttributes Attributes {get; set;}
Exceptions
Exception Type Condition
FileNotFoundException The caller attempts to access a file that does not exist on disk.
SecurityException The caller does not have the required permission.
ArgumentException The caller attempts to set an invalid file attribute.
IOException FileSystemInfo.Refresh cannot initialize the data.
Remarks
The value of this property is a combination of the archive, compressed, directory, hidden, offline, read-only, system, and temporary file attribute flags.
.NET Framework Security:
FileIOPermissionAccess Write permission required to set the attributes for the specified file.

Return to top


Property: CreationTime (read-write)
Inherited
See base class member description: System.IO.FileSystemInfo.CreationTime

Summary
Gets or sets the creation time of the current FileSystemInfo object.
C# Syntax:
public DateTime CreationTime {get; set;}
Exceptions
Exception Type Condition
IOException FileSystemInfo.Refresh cannot initialize the data.
FileNotFoundException The FileSystemInfo object does not exist.
Remarks
This property value is null if the file system containing the FileSystemInfo object does not support this information.

Return to top


Overridden Property: Exists (read-only)
Summary
Gets a value indicating whether the directory exists.
C# Syntax:
public override bool Exists {get;}
Example
		static public void CopyDirectory(string SourceDirectory, string TargetDirectory)
		{
			DirectoryInfo	source = new DirectoryInfo(SourceDirectory);
			DirectoryInfo	target = new DirectoryInfo(TargetDirectory);
			
			//Check If we have valid source
			if(!source.Exists)
				return;

			if(!target.Exists)
				target.Create();
			
			//Copy Files
			FileInfo[] sourceFiles = source.GetFiles();	
			for(int i = 0; i < sourceFiles.Length; ++i)
				File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name,true);
			
			//Copy directories
			DirectoryInfo[] sourceDirectories = source.GetDirectories();	
			for(int j = 0; j < sourceDirectories.Length; ++j)
				CopyDirectory(sourceDirectories[j].FullName,target.FullName +"\\" + sourceDirectories[j].Name);
		}

    

Return to top


Property: Extension (read-only)
Inherited
See base class member description: System.IO.FileSystemInfo.Extension

Summary
Gets the string representing the extension part of the file.
C# Syntax:
public string Extension {get;}
Remarks
The Extension property returns the FileSystemInfo extension, including the period (.). For example, for a file c:\NewFile.txt, this property returns ".txt".

Return to top


Property: FullName (read-only)
Inherited
See base class member description: System.IO.FileSystemInfo.FullName

Summary
Gets the full path of the directory or file.
C# Syntax:
public virtual string FullName {get;}
Remarks
For example, for a file c:\NewFile.txt, this property returns "NewFile.txt".
.NET Framework Security:
FileIOPermission for access to the path. Associated enumeration: FileIOPermissionAccess.PathDiscovery

Return to top


Property: LastAccessTime (read-write)
Inherited
See base class member description: System.IO.FileSystemInfo.LastAccessTime

Summary
Gets or sets the time the current file or directory was last accessed.
C# Syntax:
public DateTime LastAccessTime {get; set;}
Exceptions
Exception Type Condition
IOException FileSystemInfo.Refresh cannot initialize the data.
Remarks
This property value is null if the file system containing the file does not support this information.

Return to top


Property: LastWriteTime (read-write)
Inherited
See base class member description: System.IO.FileSystemInfo.LastWriteTime

Summary
Gets or sets the time when the current file or directory was last written to.
C# Syntax:
public DateTime LastWriteTime {get; set;}
Exceptions
Exception Type Condition
IOException FileSystemInfo.Refresh cannot initialize the data.
Remarks
This property value is null if the file system containing the file does not support this information.

Return to top


Overridden Property: Name (read-only)
Summary
Gets the name of this DirectoryInfo instance.
C# Syntax:
public override string Name {get;}
Remarks
This Name property returns only the name of the directory, such as "Bin." To get the full path, such as "c:\public\Bin", use the FileSystemInfo.FullName property.
Example
The following code example outputs the name of the current DirectoryInfo only.
 using System;
 using System.IO;
 
// This code outputs the name of the current DirectoryInfo only.
 class GetAName
 {
 public static void Main(string[] args)
 {
 DirectoryInfo dir = new DirectoryInfo(".");
 String dirName=dir.Name;
 Console.WriteLine("DirectoryInfo name is {0}", dirName);
 }
 }
 

    
See also:
FileSystemInfo.FullName

Return to top


Property: Parent (read-only)
Summary
Gets the parent directory of a specified subdirectory.
C# Syntax:
public DirectoryInfo Parent {get;}
Exceptions
Exception Type Condition
SecurityException The caller does not have the required permission.
Example
using System;
using System.IO;

public class MoveToTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // create the directory, only if it doesn't exist
        if (di.Exists == false)
            di.Create();

        // create a subdirectory in the directory just created
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // get a reference to the parent directory of the subdir we just made
        DirectoryInfo parentDir = dis.Parent;

        Console.WriteLine("The parent directory of '{0}' is '{1}'", dis.Name, parentDir.Name);

        // delete the major directory
        di.Delete(true);
    }
}

    
.NET Framework Security:
FileIOPermission for reading from files and directories and for access to the path. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery

Return to top


Property: Root (read-only)
Summary
Gets the root portion of a path.
C# Syntax:
public DirectoryInfo Root {get;}
Example
using System;
using System.IO;

public class MoveToTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // create the directory, only if it doesn't exist
        if (di.Exists == false)
            di.Create();

        // create a subdirectory in the directory just created
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        Console.WriteLine("The root path of '{0}' is '{1}'", dis.Name, dis.Root);

        // delete the major directory
        di.Delete(true);
    }
}

    

Return to top


Method: Create()
Summary
Creates a directory.
C# Syntax:
public void Create();
Remarks
By default, full read/write access to new directories is granted to all users.

If the directory already exists, this method does nothing.

.NET Framework Security:
FileIOPermission for writing files. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Method: CreateSubdirectory(
   string path
)
Summary
Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.
C# Syntax:
public DirectoryInfo CreateSubdirectory(
   string path
);
Parameters:

path

The specified path. This cannot be a different disk volume or Universal Naming Convention (UNC) name.

Return Value:
The last directory specified in path.
Exceptions
Exception Type Condition
ArgumentException path does not specify a valid file path or contains invalid DirectoryInfo characters.
ArgumentNullException path is null.
DirectoryNotFoundException Part of the path is not found.
IOException A file or directory already has the name specified by path.
PathTooLongException The specified path, file name, or both are too long. After full qualification, each must be less than 256 characters.
SecurityException The caller does not have the required permission.
Remarks
Any and all directories specified in path are created, unless some part of path is invalid. The path parameter specifies a directory path, not a file path. If the subdirectory already exists, this method does nothing.

By default, full read/write access to new directories is granted to all users.



Note Path names are limited to 248 characters.
Example
using System;
using System.IO;

public class CreateSubTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // create the directory, only if it doesn't exist
        if (di.Exists == false)
            di.Create();

        // create a subdirectory in the directory just created
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // process that directory as required
        // ...

        // delete the subdirectory
        dis.Delete(true);

        // delete the directory
        di.Delete(true);
    }
}

    
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Delete()
Summary
Deletes this DirectoryInfo if it is empty.
C# Syntax:
public override void Delete();
Exceptions
Exception Type Condition
IOException The directory is not empty.
SecurityException The caller does not have the required permission.
.NET Framework Security:
FileIOPermission for writing directories. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Overloaded Method: Delete(
   bool recursive
)
Summary
Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
C# Syntax:
public void Delete(
   bool recursive
);
Parameters:

recursive

true to delete this DirectoryInfo, its subdirectories, and all files; otherwise false.

Exceptions
Exception Type Condition
IOException The DirectoryInfo is read-only, or the DirectoryInfo contains one or more files or subdirectories and recursive is false.
SecurityException The caller does not have the required permission.
Remarks
If the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. Attempting to delete a DirectoryInfo that is not empty when recursive is false throws an IOException.
Example
using System;
using System.IO;

public class DeleteTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // create the directory, only if it doesn't exist
        if (di.Exists == false)
            di.Create();

        // create a subdirectory in the directory just created
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // process that directory as required
        // ...

        // delete the subdirectory. The true indicates that if subdirectories
        // or files are in this directory, delete them as well
        dis.Delete(true);

        // delete the directory
        di.Delete(true);
    }
}

    
.NET Framework Security:
FileIOPermissionAccess Write flag required for write permission to the DirectoryInfo and subdirectories to be deleted.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DirectoryInfo();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetDirectories()
Summary
Returns the subdirectories of the current directory.
C# Syntax:
public DirectoryInfo[] GetDirectories();
Return Value:
An array of DirectoryInfo objects.
Exceptions
Exception Type Condition
DirectoryNotFoundException The path encapsulated in the DirectoryInfo object does not exist.
Remarks
If there are no subdirectories, this method returns only the root directory. This method is not recursive.
Example
using System;
using System.IO;

public class GetDirectoriesTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // get a reference to each directory in that directory
        DirectoryInfo[] diArr = di.GetDirectories();

        // print out the names of the directories
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}

    
.NET Framework Security:
FileIOPermission for reading directories. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Overloaded Method: GetDirectories(
   string searchPattern
)
Summary
Returns an array of directories in the current DirectoryInfo matching the given search criteria.
C# Syntax:
public DirectoryInfo[] GetDirectories(
   string searchPattern
);
Parameters:

searchPattern

The search string, such as "System*", used to search for all directories beginning with the word "System".

Return Value:
An array of type DirectoryInfo matching searchPattern.
Exceptions
Exception Type Condition
ArgumentNullException searchPattern is null.
DirectoryNotFoundException The path encapsulated in the Directory object does not exist.
SecurityException The caller does not have the required permission.
Remarks
Wild cards are permitted. For example, the searchPattern string "*t" searches for all directory names in path ending with the letter "t". The searchPattern string "s*" searches for all directory names in path beginning with the letter "s".

The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". It cannot be used to move up the directory hierarchy.

If there are no subdirectories, or no subdirectories are found that match searchPattern, this method returns only the root directory.

.NET Framework Security:
FileIOPermission for reading from files and directories and for access to the path. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery

Return to top


Overloaded Method: GetFiles()
Summary
Returns a file list from the current directory.
C# Syntax:
public FileInfo[] GetFiles();
Return Value:
An array of type FileInfo.
Exceptions
Exception Type Condition
DirectoryNotFoundException The directory does not exist.
Remarks
If there are no files in the DirectoryInfo, this method returns an empty array.
Example
using System;
using System.IO;

public class GetFilesTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // get a reference to each file in that directory
        FileInfo[] fiArr = di.GetFiles();

        // print out the names of the files
        foreach (FileInfo fri in fiArr)
            Console.WriteLine(fri.Name);
    }
}

    
.NET Framework Security:
FileIOPermission for reading directories. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Overloaded Method: GetFiles(
   string searchPattern
)
Summary
Returns a file list from the current directory matching the given searchPattern, such as "*.txt".
C# Syntax:
public FileInfo[] GetFiles(
   string searchPattern
);
Parameters:

searchPattern

The search string, such as "*.txt".

Return Value:
An array of type FileInfo.
Exceptions
Exception Type Condition
ArgumentNullException searchPattern is null.
DirectoryNotFoundException The directory does not exist.
SecurityException The caller does not have the required permission.
Remarks
Wild cards are permitted. For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". The searchPattern string "s*" searches for all file names beginning with the letter "s". If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.
.NET Framework Security:
FileIOPermission for access to the path and for reading from files and directories. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery

Return to top


Overloaded Method: GetFileSystemInfos()
Summary
Returns an array of strongly typed FileSystemInfo entries listing all the files and directories.
C# Syntax:
public FileSystemInfo[] GetFileSystemInfos();
Return Value:
An array of strongly typed FileSystemInfo entries.
Remarks
If there are no files or directories in the DirectoryInfo, this method returns an empty array.
See also:
FileSystemInfo

Return to top


Overloaded Method: GetFileSystemInfos(
   string searchPattern
)
Summary
Retrieves an array of strongly typed FileSystemInfo objects matching the specified search criteria.
C# Syntax:
public FileSystemInfo[] GetFileSystemInfos(
   string searchPattern
);
Parameters:

searchPattern

The search string, such as "System*", used to search for all directories beginning with the word "System".

Return Value:
An array of strongly typed FileSystemInfo objects matching the search criteria.
Exceptions
Exception Type Condition
ArgumentNullException searchPattern is null.
SecurityException The caller does not have the required permission.
Remarks
Wild cards are permitted. For example, the searchPattern string "*t" searches for all directory names in path ending with the letter "t". The searchPattern string "s*" searches for all directory names in path beginning with the letter "s". The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". It cannot be used to move up the directory hierarchy. If there are no files or directories, or no files or directories that match the searchPattern string in the DirectoryInfo, this method returns an empty array.
.NET Framework Security:
FileIOPermission for reading from files and directories and for access to the path. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery
See also:
FileSystemInfo

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MoveTo(
   string destDirName
)
Summary
Moves a DirectoryInfo instance and its contents to a new path.
C# Syntax:
public void MoveTo(
   string destDirName
);
Parameters:

destDirName

The name and path to which to move this DirectoryInfo instance. The destDirName parameter cannot be another disk volume or a directory with the identical name. It can be an existing directory to which you want to add this DirectoryInfo instance as a subdirectory.

Exceptions
Exception Type Condition
ArgumentNullException destDirName is null.
ArgumentException destDirName is an empty string (''").
IOException An attempt was made to move a directory to a different volume, or destDirName already exists.
SecurityException The caller does not have the required permission.
Remarks
This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

This method permits moving a directory to a read-only directory. The read/write attribute of neither directory is affected.

Example
using System;
using System.IO;

public class MoveToTest {
    public static void Main() {

        // make a reference to a directory
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // create the directory, only if it doesn't exist
        if (di.Exists == false)
            di.Create();

        // create a subdirectory in the directory just created
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // move the main directory. Note that it's contents move also
        if (Directory.Exists("NewTempDir") == false)
            di.MoveTo("NewTempDir");

        try {
            // attempt to delete the subdirectory. Note that because it has been
            // moved, an exception will be thrown
            dis.Delete(true);
        } catch (Exception) {
            // handle this exception. This is for demonstration however.
            // Console.WriteLine("That directory did not exist");
        }

        // point our directoryinfo reference to the new directory
        //di = new DirectoryInfo("NewTempDir");

        // delete the directory
        //di.Delete(true);
    }
}

    
.NET Framework Security:
FileIOPermissionAccess Write flag required for write permission to the destination directory.

Return to top


Method: Refresh()
Inherited
See base class member description: System.IO.FileSystemInfo.Refresh

Summary
Refreshes the state of the object.
C# Syntax:
public void Refresh();
Exceptions
Exception Type Condition
ArgumentException The file is not found.
IOException A device such as a disk drive is not ready.
Remarks
FileSystemInfo.Refresh takes a snapshot of the file from the current file system.Refresh cannot correct the underlying file system even if the file system returns incorrect or outdated information. This can happen on platforms such as Windows 98.

Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated.

Return to top


Overridden Method: ToString()
Summary
Returns the original path that was passed by the user.
C# Syntax:
public override string ToString();
Return Value:
Returns the original path that was passed by the user.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.