FileInfo(String) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 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
。
呼叫端沒有必要的權限。
.NET Framework 和 2.1 之前的 .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...
備註
您可以指定完整或相對檔名,但安全性檢查會取得完整名稱。