İngilizce dilinde oku

Aracılığıyla paylaş


FileInfo.Open Yöntem

Tanım

Çeşitli okuma/yazma ve paylaşma ayrıcalıklarına sahip bir dosya açar.

Aşırı Yüklemeler

Open(FileMode)

Belirtilen modda bir dosya açar.

Open(FileStreamOptions)

Sınıfın FileStream yeni bir örneğini belirtilen oluşturma modu, okuma/yazma ve paylaşma izniyle başlatır, diğer FileStream'lerin aynı dosyaya, arabellek boyutuna, ek dosya seçeneklerine ve ayırma boyutuna erişmesi gerekebilir.

Open(FileMode, FileAccess)

Bir dosyayı belirtilen modda okuma, yazma veya okuma/yazma erişimiyle açar.

Open(FileMode, FileAccess, FileShare)

Bir dosyayı belirtilen modda okuma, yazma veya okuma/yazma erişimiyle ve belirtilen paylaşım seçeneğiyle açar.

Open(FileMode)

Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs

Belirtilen modda bir dosya açar.

C#
public System.IO.FileStream Open (System.IO.FileMode mode);

Parametreler

mode
FileMode

FileMode Dosyanın açıldığı modu (örneğin, Open veya Append) belirten bir sabit.

Döndürülenler

Okuma/yazma erişimi olan ve paylaşılmayan belirtilen modda açılmış bir dosya.

Özel durumlar

Dosya bulunamadı.

Dosya salt okunur veya bir dizindir.

Belirtilen yol, eşlenmemiş bir sürücüde olmak gibi geçersiz.

Dosya zaten açık.

Örnekler

Aşağıdaki örnek bir dosyayı açar, dosyaya bazı bilgiler ekler ve dosyayı okur.

C#
using System;
using System.IO;
using System.Text;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        // Delete the file if it exists.
        if (!fi.Exists)
        {
            //Create the file.
            using (FileStream fs = fi.Create())
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                //Add some information to the file.
                fs.Write(info, 0, info.Length);
            }
        }

        //Open the stream and read it back.
        using (FileStream fs = fi.Open(FileMode.Open))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
//
//
//
//
//
//
//
//
//
//
//

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.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

Open(FileStreamOptions)

Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs

Sınıfın FileStream yeni bir örneğini belirtilen oluşturma modu, okuma/yazma ve paylaşma izniyle başlatır, diğer FileStream'lerin aynı dosyaya, arabellek boyutuna, ek dosya seçeneklerine ve ayırma boyutuna erişmesi gerekebilir.

C#
public System.IO.FileStream Open (System.IO.FileStreamOptions options);

Parametreler

options
FileStreamOptions

Kullanılacak isteğe bağlı FileStream parametreleri açıklayan bir nesne.

Döndürülenler

FileStream Açılan dosyayı sarmalayan bir.

Açıklamalar

FileStream(String, FileStreamOptions) özel durumlar hakkında bilgi için.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET 6, 7, 8, 9

Open(FileMode, FileAccess)

Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs

Bir dosyayı belirtilen modda okuma, yazma veya okuma/yazma erişimiyle açar.

C#
public System.IO.FileStream Open (System.IO.FileMode mode, System.IO.FileAccess access);

Parametreler

mode
FileMode

FileMode Dosyanın açıldığı modu (örneğin, Open veya Append) belirten bir sabit.

access
FileAccess

FileAccess Dosyanın , Writeveya ReadWrite dosya erişimiyle Readaçılmasını belirten sabit.

Döndürülenler

FileStream Belirtilen modda açılan ve erişimi olan ve paylaşılmayan bir nesne.

Özel durumlar

Çağıranın gerekli izni yok.

Dosya bulunamadı.

Name salt okunur veya bir dizindir.

Belirtilen yol, eşlenmemiş bir sürücüde olmak gibi geçersiz.

Dosya zaten açık.

Name boş veya yalnızca boşluk içeriyor.

Bir veya daha fazla bağımsız değişken null.

Örnekler

Aşağıdaki örnek bir dosyayı salt okunur olarak açar ve dosyadan okur.

C#
using System;
using System.IO;
using System.Text;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\MyTest.txt";
        FileInfo fi = new FileInfo(path);

        // Delete the file if it exists.
        if (!fi.Exists)
        {
            //Create the file.
            using (FileStream fs = fi.Create())
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                //Add some information to the file.
                fs.Write(info, 0, info.Length);
            }
        }

        //Open the stream and read it back.
        using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Read))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }

            try
            {
                //Try to write to the file.
                fs.Write(b,0,b.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine("Writing was disallowed, as expected: {0}",
                    e.ToString());
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is some text in the file.
//
//
//
//
//
//
//
//
//
//
//
//
//Writing was disallowed, as expected: System.NotSupportedException: Stream does
//not support writing.
//   at System.IO.__Error.WriteNotSupported()
//   at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
//   at Test.Main() in C:\Documents and Settings\My Computer\My Documents\
//Visual Studio 2005\Projects\finfo open2\finfo open2\Program.cs:line 39

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.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

Open(FileMode, FileAccess, FileShare)

Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs
Kaynak:
FileInfo.cs

Bir dosyayı belirtilen modda okuma, yazma veya okuma/yazma erişimiyle ve belirtilen paylaşım seçeneğiyle açar.

C#
public System.IO.FileStream Open (System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share);

Parametreler

mode
FileMode

FileMode Dosyanın açıldığı modu (örneğin, Open veya Append) belirten bir sabit.

access
FileAccess

FileAccess Dosyanın , Writeveya ReadWrite dosya erişimiyle Readaçılmasını belirten sabit.

share
FileShare

FileShare Diğer FileStream nesnelerin bu dosyaya sahip olduğu erişim türünü belirten bir sabit.

Döndürülenler

FileStream Belirtilen mod, erişim ve paylaşım seçenekleriyle açılmış bir nesne.

Özel durumlar

Çağıranın gerekli izni yok.

Dosya bulunamadı.

Name salt okunur veya bir dizindir.

Belirtilen yol, eşlenmemiş bir sürücüde olmak gibi geçersiz.

Dosya zaten açık.

Name boş veya yalnızca boşluk içeriyor.

Bir veya daha fazla bağımsız değişken null.

Örnekler

Aşağıdaki örnekte, okuma ve yazma için bir dosyanın açılması, ancak diğer kullanıcılara veya işlemlere erişime izin verme işlemi gösterilmektedir.

C#
using System;
using System.IO;

public class OpenTest
{
    public static void Main()
    {
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo("temp.txt");

        // Open the file just specified such that no one else can use it.
        FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None );

        // Create another reference to the same file.
        FileInfo nextfi = new FileInfo("temp.txt");

        try
        {
            // Try opening the same file, which was locked by the previous process.
            nextfi.Open( FileMode.OpenOrCreate, FileAccess.Read );

            Console.WriteLine("The file was not locked, and was opened by a second process.");
        }
        catch (IOException)
        {
            Console.WriteLine("The file could not be opened because it was locked by another process.");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        // Close the file so it can be deleted.
        fs.Close();
    }
}

//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//The file could not be opened because it was locked by another process.

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.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