Auf Englisch lesen

Freigeben über


FileInfo(String) Konstruktor

Definition

Initialisiert eine neue Instanz der FileInfo-Klasse, die als Wrapper für einen Dateipfad fungiert.

public FileInfo (string fileName);

Parameter

fileName
String

Der vollqualifizierte Name der neuen Datei oder der relative Dateiname. Der Pfad darf nicht mit dem Verzeichnistrennzeichen enden.

Ausnahmen

fileName ist null.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

.NET Framework- und .NET Core-Versionen älter als 2.1: Der Dateiname ist leer, enthält nur Leerzeichen oder ungültige Zeichen.

Der Zugriff auf fileName wurde verweigert.

Der angegebene Pfad und/oder Dateiname überschreiten die vom System definierte maximale Länge.

fileName enthält einen Doppelpunkt (:) innerhalb der Zeichenfolge.

Beispiele

Im folgenden Beispiel wird dieser Konstruktor verwendet, um zwei Dateien zu erstellen, die dann in geschrieben, gelesen, kopiert und gelöscht werden.

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());
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Hello
//And
//Welcome
//c:\MyTest.txt was copied to c:\MyTest.txttemp.
//c:\MyTest.txttemp was successfully deleted.

Im folgenden Beispiel wird eine vorhandene Datei geöffnet oder eine Datei erstellt, Text an die Datei angefügt und die Ergebnisse angezeigt.

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() );
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Add as many lines as you like...
//Add another line to the output...
//This is a new entry to add to the file
//This is yet another line to add...

Hinweise

Sie können entweder den vollqualifizierten oder den relativen Dateinamen angeben, aber die Sicherheitsüberprüfung erhält den vollqualifizierten Namen.

Gilt für:

Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Weitere Informationen