FileInfo(String) 생성자

정의

파일 경로의 래퍼 역할을 하는 FileInfo 클래스의 새 인스턴스를 초기화합니다.

public:
 FileInfo(System::String ^ fileName);
public FileInfo (string fileName);
new System.IO.FileInfo : string -> System.IO.FileInfo
Public Sub New (fileName As String)

매개 변수

fileName
String

새 파일의 정규화된 이름이거나 상대적인 파일 이름입니다. 경로가 디렉터리 구분 기호 문자로 끝나서는 안 됩니다.

예외

fileName이(가) null인 경우

호출자에게 필요한 권한이 없는 경우

2.1보다 오래된 .NET Framework 및 .NET Core 버전: 파일 이름이 비어 있거나 공백만 포함하거나 잘못된 문자를 포함합니다.

fileName에 대한 액세스가 거부되었습니다.

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

fileName의 문자열 중간에 콜론(:)이 있습니다.

예제

다음 예제에서는 이 생성자를 사용하여 두 개의 파일을 만든 다음, 기록, 읽기, 복사 및 삭제합니다.

using namespace System;
using namespace System::IO;

int main()
{
   String^ path = "c:\\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 );
   }
}

//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.
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.
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
'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.

다음 예제에서는 기존 파일을 열거나 파일을 만들고, 파일에 텍스트를 추가하고, 결과를 표시합니다.

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

설명

정규화된 파일 이름 또는 상대 파일 이름을 지정할 수 있지만 보안 검사에서 정규화된 이름을 가져옵니다.

적용 대상

추가 정보