当前位置: 首页>>代码示例>>C#>>正文


C# FileInfo.Open方法代码示例

本文整理汇总了C#中System.IO.FileInfo.Open方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.Open方法的具体用法?C# FileInfo.Open怎么用?C# FileInfo.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileInfo的用法示例。


在下文中一共展示了FileInfo.Open方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
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.
//
//
//
//
//
//
//
//
//
//
//
//
开发者ID:.NET开发者,项目名称:System.IO,代码行数:53,代码来源:FileInfo.Open

示例2: Main

//引入命名空间
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
开发者ID:.NET开发者,项目名称:System.IO,代码行数:70,代码来源:FileInfo.Open

示例3: Main

//引入命名空间
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.
开发者ID:.NET开发者,项目名称:System.IO,代码行数:42,代码来源:FileInfo.Open

示例4: Main

//引入命名空间
using System;
using System.IO;

class FileFileInfoApp
{
    static void Main(string[] args)
    {
        FileInfo f = new FileInfo("Bar.txt");
        FileStream fs = f.Create();
   
        StreamWriter w = new StreamWriter(fs);
        w.Write("Hello World");
        w.Close();
   
        fs = f.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        StreamReader r = new StreamReader(fs);
        string t;
        while ((t = r.ReadLine()) != null){
            Console.WriteLine(t);
        }
        w.Close();
        fs.Close();
        f.Delete();
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:26,代码来源:FileInfo.Open


注:本文中的System.IO.FileInfo.Open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。