Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Initialisiert eine neue Instanz der FileInfo-Klasse, die als Wrapper für einen Dateipfad fungiert.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Sub New ( _
fileName As String _
)
'Usage
Dim fileName As String
Dim instance As New FileInfo(fileName)
public FileInfo (
string fileName
)
public:
FileInfo (
String^ fileName
)
public FileInfo (
String fileName
)
public function FileInfo (
fileName : String
)
Parameter
- fileName
Der vollqualifizierte Name der neuen Datei oder der relative Dateiname.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
fileName ist NULL (Nothing in Visual Basic). |
|
Der Aufrufer verfügt nicht über die erforderliche Berechtigung. |
|
Der Dateiname ist leer, oder er enthält nur Leerräume oder ungültige Zeichen. |
|
Der Zugriff auf fileName wird verweigert. |
|
Der angegebene Pfad und/oder der Dateiname überschreiten die vom System vorgegebene Höchstlänge. Beispielsweise dürfen auf Windows-Plattformen Pfade nicht länger als 247 Zeichen und Dateinamen nicht länger als 259 Zeichen sein. |
|
fileName enthält einen Doppelpunkt (:) innerhalb der Zeichenfolge. |
Hinweise
Sie können den vollqualifizierten oder den relativen Dateinamen angeben, die Sicherheitsüberprüfung ruft jedoch den vollqualifizierten Namen ab.
In der folgenden Tabelle sind Beispiele für andere typische oder verwandte E/A-Aufgaben aufgeführt.
Aufgabe |
Beispiel in diesem Thema |
|---|---|
Erstellen einer Textdatei. |
|
Schreiben in eine Textdatei. |
|
Lesen aus einer Textdatei. |
|
Anfügen von Text an eine Datei. |
|
Umbenennen oder Verschieben einer Datei. |
|
Löschen einer Datei. |
|
Kopieren einer Datei. |
|
Abrufen der Größe einer Datei. |
|
Abrufen der Attribute einer Datei. |
|
Festlegen der Attribute einer Datei. |
|
Bestimmen, ob eine Datei vorhanden ist. |
|
Lesen aus einer Binärdatei. |
Gewusst wie: Lesen und Schreiben einer neu erstellten Datendatei |
Schreiben in eine Binärdatei. |
Gewusst wie: Lesen und Schreiben einer neu erstellten Datendatei |
Abrufen einer Dateierweiterung. |
|
Abrufen des vollqualifizierten Pfads einer Datei. |
|
Abrufen des Dateinamens sowie der Dateierweiterung aus einem Pfad. |
|
Ändern der Erweiterung einer Datei. |
Beispiel
Im folgenden Beispiel werden mit diesem Konstruktor zwei Dateien erstellt. Anschließend wird in diese geschrieben, aus ihnen gelesen, sie werden kopiert und gelöscht.
Imports System
Imports System.IO
Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fi1 As FileInfo = New FileInfo(path)
If fi1.Exists = False Then
'Create a file to write to.
Dim sw As StreamWriter = fi1.CreateText()
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
End If
'Open the file to read from.
Dim sr As StreamReader = fi1.OpenText()
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Loop
Try
Dim path2 As String = path + "temp"
Dim fi2 As FileInfo = New FileInfo(path2)
'Ensure that the target does not exist.
fi2.Delete()
'Copy the file.
fi1.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
'Delete the newly created file.
fi2.Delete()
Console.WriteLine("{0} was successfully deleted.", path2)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
FileInfo fi1 = new FileInfo(path);
if (!fi1.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi1.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
try
{
string path2 = path + "temp";
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
using namespace System;
using namespace System::IO;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
FileInfo^ fi1 = gcnew FileInfo( path );
if ( !fi1->Exists )
{
//Create a file to write to.
StreamWriter^ sw = fi1->CreateText();
try
{
sw->WriteLine( "Hello" );
sw->WriteLine( "And" );
sw->WriteLine( "Welcome" );
}
finally
{
if ( sw )
delete (IDisposable^)sw;
}
}
//Open the file to read from.
StreamReader^ sr = fi1->OpenText();
try
{
String^ s = "";
while ( s = sr->ReadLine() )
{
Console::WriteLine( s );
}
}
finally
{
if ( sr )
delete (IDisposable^)sr;
}
try
{
String^ path2 = String::Concat( path, "temp" );
FileInfo^ fi2 = gcnew FileInfo( path2 );
//Ensure that the target does not exist.
fi2->Delete();
//Copy the file.
fi1->CopyTo( path2 );
Console::WriteLine( "{0} was copied to {1}.", path, path2 );
//Delete the newly created file.
fi2->Delete();
Console::WriteLine( "{0} was successfully deleted.", path2 );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
String path = "c:\\temp\\MyTest.txt";
FileInfo fi1 = new FileInfo(path);
if (!(fi1.get_Exists())) {
//Create a file to write to.
StreamWriter sw = fi1.CreateText();
try {
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
finally {
sw.Dispose();
}
}
//Open the file to read from.
StreamReader sr = fi1.OpenText();
try {
String s = "";
while ((s = sr.ReadLine())!= null) {
Console.WriteLine(s);
}
}
finally {
sr.Dispose();
}
try {
String path2 = path + "temp";
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
} //main
} //Test
Im folgenden Beispiel wird eine vorhandene Datei geöffnet bzw. eine Datei erstellt, Text an die Datei angefügt, und die Ergebnisse werden angezeigt.
Imports System
Imports System.IO
Public Class FileInfoMainTest
Public Shared Sub Main()
' Open an existing file, or create a new one.
Dim fi As New FileInfo("temp.txt")
' Create a writer, ready to add entries to the file.
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("This is a new entry to add to the file")
sw.WriteLine("This is yet another line to add...")
sw.Flush()
sw.Close()
Dim sr As New StreamReader(fi.OpenRead())
' Get the information out of the file and display it.
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
End Sub 'Main
End Class 'FileInfoMainTest
using System;
using System.IO;
public class FileInfoMainTest
{
public static void Main()
{
// Open an existing file, or create a new one.
FileInfo fi = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
StreamWriter sw = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
StreamReader sr = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
using namespace System;
using namespace System::IO;
int main()
{
// Open an existing file, or create a new one.
FileInfo^ fi = gcnew FileInfo( "temp.txt" );
// Create a writer, ready to add entries to the file.
StreamWriter^ sw = fi->AppendText();
sw->WriteLine( "This is a new entry to add to the file" );
sw->WriteLine( "This is yet another line to add..." );
sw->Flush();
sw->Close();
// Get the information out of the file and display it.
StreamReader^ sr = gcnew StreamReader( fi->OpenRead() );
while ( sr->Peek() != -1 )
Console::WriteLine( sr->ReadLine() );
}
import System;
import System.IO;
public class FileInfoMainTest {
public static function Main() : void {
// Open an existing file, or create a new one.
var fi : FileInfo = new FileInfo("temp.txt");
// Create a writer, ready to add entries to the file.
var sw : StreamWriter = fi.AppendText();
sw.WriteLine("This is a new entry to add to the file");
sw.WriteLine("This is yet another line to add...");
sw.Flush();
sw.Close();
// Get the information out of the file and display it.
var sr : StreamReader = new StreamReader( fi.OpenRead() );
while (sr.Peek() != -1)
Console.WriteLine( sr.ReadLine() );
}
}
FileInfoMainTest.Main();
.NET Framework-Sicherheit
- FileIOPermission zum Lesen von Dateien. Zugeordnete Enumeration: FileIOPermissionAccess.Read
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
FileInfo-Klasse
FileInfo-Member
System.IO-Namespace
Weitere Ressourcen
Datei- und Stream-E/A
Gewusst wie: Lesen aus einer Textdatei
Gewusst wie: Schreiben von Text in eine Datei