親ディレクトリのインスタンスを取得します。
Public ReadOnly Property Directory As DirectoryInfo
[C#]
public DirectoryInfo Directory {get;}
[C++]
public: __property DirectoryInfo* get_Directory();
[JScript]
public function get Directory() : DirectoryInfo;
プロパティ値
ファイルの親ディレクトリを表す DirectoryInfo オブジェクト。
例外
例外の種類 | 条件 |
---|---|
DirectoryNotFoundException | 割り当てられていないドライブであるなど、指定されたパスが無効です。 |
SecurityException | 呼び出し元に、必要なアクセス許可がありません。 |
解説
親ディレクトリを文字列として取得するには、 DirectoryName プロパティを使用します。
このプロパティの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク | 参考例があるトピック |
---|---|
ディレクトリをコピーする。 | Directory |
ディレクトリの名前を変更、またはディレクトリを移動する。 | Directory.Move |
ディレクトリを削除する。 | Directory.Delete |
ディレクトリを作成する。 | CreateDirectory
Directory |
サブディレクトリを作成する。 | CreateSubdirectory |
ディレクトリ内のファイルを参照する。 | Name |
ディレクトリ内のサブディレクトリを参照する。 | GetDirectories |
ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 | GetFileSystemInfos |
ディレクトリのサイズを取得する。 | Directory |
ファイルが存在するかどうかを判別する。 | Exists |
ディレクトリが存在するかどうかを判別する。 | Exists |
使用例
ファイルを開くか作成して、その絶対パスを判別し、ディレクトリの完全な内容を判別して表示する例を次に示します。
Imports System
Imports System.IO
Public Class DirectoryTest
Public Shared Sub Main()
' Open an existing file, or create a new one.
Dim fi As New FileInfo("temp.txt")
' Determine the full path of the file just created.
Dim di As DirectoryInfo = fi.Directory
' Figure out what other entries are in that directory.
Dim fsi As FileSystemInfo() = di.GetFileSystemInfos()
' Print the names of all the files and subdirectories of that directory.
Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName)
Dim info As FileSystemInfo
For Each info In fsi
Console.WriteLine(info.Name)
Next info
End Sub 'Main
End Class 'DirectoryTest
[C#]
using System;
using System.IO;
public class DirectoryTest
{
public static void Main()
{
// Open an existing file, or create a new one.
FileInfo fi = new FileInfo("temp.txt");
// Determine the full path of the file just created.
DirectoryInfo di = fi.Directory;
// Figure out what other entries are in that directory.
FileSystemInfo[] fsi = di.GetFileSystemInfos();
Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);
// Print the names of all the files and subdirectories of that directory.
foreach (FileSystemInfo info in fsi)
Console.WriteLine(info.Name);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
// Open an existing file, or create a new one.
FileInfo* fi = new FileInfo(S"temp.txt");
// Determine the full path of the file just created.
DirectoryInfo* di = fi->Directory;
// Figure out what other entries are in that directory.
FileSystemInfo* fsi[] = di->GetFileSystemInfos();
Console::WriteLine(S"The directory '{0}' contains the following files and directories:", di->FullName);
// Print the names of all the files and subdirectories of that directory.
Collections::IEnumerator* myEnum = fsi->GetEnumerator();
while (myEnum->MoveNext()) {
FileSystemInfo* info = __try_cast<FileSystemInfo*>(myEnum->Current);
Console::WriteLine(info->Name);
}
}
[JScript]
import System;
import System.IO;
public class DirectoryTest {
public static function Main() : void {
// Open an existing file, or create a new one.
var fi : FileInfo = new FileInfo("temp.txt");
// Determine the full path of the file just created.
var di : DirectoryInfo = fi.Directory;
// Figure out what other entries are in that directory.
var fsi : FileSystemInfo[] = di.GetFileSystemInfos();
Console.WriteLine("The directory '{0}' contains the following files and directories:", di.FullName);
// Print the names of all the files and subdirectories of that directory.
for (var i : int in fsi)
Console.WriteLine(fsi[i].Name);
}
}
DirectoryTest.Main();
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
.NET Framework セキュリティ:
- FileIOPermission (ファイルの読み取り用) FileIOPermissionAccess.Read (関連する列挙体)
参照
FileInfo クラス | FileInfo メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み