簡體   English   中英

如何以只讀模式打開文本文件,這意味着我無法執行寫操作?

[英]How to open a text file in read-only mode means there i can not perform write?

我嘗試了太多時間,但無法實現目標。 文件已打開,但以寫模式打開。

在txtpath.text中編碼,我正在傳遞文本的路徑:

System.IO.FileInfo fileObj= new System.IO.FileInfo(txtPath.Text);

fileObj.Attributes = System.IO.FileAttributes.ReadOnly;

System.Diagnostics.Process.Start(fileObj.FullName);

使用File.OpenRead方法

 string sFilename = "myfile.txt";
 FileStream SR = File.OpenRead(sFilename);

打開文件僅讀取其內容:

    // Open the stream and read it back.
    using (FileStream fs = File.OpenRead(path)) 
    {
        byte[] b = new byte[1024];
        UTF8Encoding temp = new UTF8Encoding(true);

        while (fs.Read(b,0,b.Length) > 0) 
        {
            Console.WriteLine(temp.GetString(b));
        }
    }

有關File.OpenRead更多信息: http : //msdn.microsoft.com/zh-cn/library/system.io.file.openread.aspx

設置文件的ReadOnly屬性並執行:

File.SetAttributes(txtPath.Text, File.GetAttributes(txtPath.Text) | FileAttributes.ReadOnly);
System.Diagnostics.Process.Start(txtPath.Text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM