当前位置: 首页>>代码示例>>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;未经允许,请勿转载。