當前位置: 首頁>>代碼示例>>C#>>正文


C# FileStream類代碼示例

本文整理匯總了C#中System.IO.FileStream的典型用法代碼示例。如果您正苦於以下問題:C# FileStream類的具體用法?C# FileStream怎麽用?C# FileStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FileStream類屬於System.IO命名空間,在下文中一共展示了FileStream類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.IO;
using System.Text;

class Test
{

    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        //Create the file.
        using (FileStream fs = File.Create(path))
        {
            AddText(fs, "This is some text");
            AddText(fs, "This is some more text,");
            AddText(fs, "\r\nand this is on a new line");
            AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");

            for (int i=1;i < 120;i++)
            {
                AddText(fs, Convert.ToChar(i).ToString());
            }
        }

        //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));
            }
        }
    }

    private static void AddText(FileStream fs, string value)
    {
        byte[] info = new UTF8Encoding(true).GetBytes(value);
        fs.Write(info, 0, info.Length);
    }
}
開發者ID:.NET開發者,項目名稱:System.IO,代碼行數:50,代碼來源:FileStream

示例2: MainWindow

//引入命名空間
using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.IO;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            UnicodeEncoding uniencoding = new UnicodeEncoding();
            string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";
           
            byte[] result = uniencoding.GetBytes(UserInput.Text);
            
            using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
            {
                SourceStream.Seek(0, SeekOrigin.End);
                await SourceStream.WriteAsync(result, 0, result.Length);
            }
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.IO,代碼行數:32,代碼來源:FileStream

示例3: new FileStream(String fileName, FileMode.Open)

//引入命名空間
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

public class MainClass
{
    public static void Main()
    {
        using (Stream from = new FileStream("c:\\test.txt", FileMode.Open))
        using (Stream to = new FileStream("c:\\test2.txt", FileMode.OpenOrCreate))
        {
            int readCount;
            byte[] buffer = new byte[1024];
            while ((readCount = from.Read(buffer, 0, 1024)) != 0)
            {
                to.Write(buffer, 0, readCount);
            }
        }
    }
}
開發者ID:C#程序員,項目名稱:System.IO,代碼行數:29,代碼來源:FileStream

示例4: new FileStream("soaptest.xml", FileMode.Create, FileAccess.ReadWrite)

//引入命名空間
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

class SoapTest
{
   public static void Main()
   {
      SerialEmployee emp1 = new SerialEmployee();
      SerialEmployee emp2 = new SerialEmployee();

      emp1.EmployeeID = 1;
      emp1.LastName = "B";
      emp1.FirstName = "K";
      emp1.YearsService = 12;
      emp1.Salary = 35000.50;

      emp2.EmployeeID = 2;
      emp2.LastName = "B";
      emp2.FirstName = "J";
      emp2.YearsService = 9;
      emp2.Salary = 23700.30;

      Stream str = new FileStream("soaptest.xml", FileMode.Create, FileAccess.ReadWrite);
      IFormatter formatter = new SoapFormatter();

      formatter.Serialize(str, emp1);
      formatter.Serialize(str, emp2);
      str.Close();
   }
}


[Serializable]
public class SerialEmployee
{
   public int EmployeeID;
   public string LastName;
   public string FirstName;
   public int YearsService;
   public double Salary;

   public SerialEmployee()
   {
      EmployeeID = 0;
      LastName = null;
      FirstName = null;
      YearsService = 0;
      Salary = 0.0;
   }
}
開發者ID:C#程序員,項目名稱:System.IO,代碼行數:53,代碼來源:FileStream


注:本文中的System.IO.FileStream類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。