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


C# FileStream.Read方法代码示例

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


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

示例1: Main

    static int Main()
    {
        byte [] buf = new byte [1];
        AsyncCallback ac = new AsyncCallback (async_callback);
        IAsyncResult ar;
        int sum0 = 0;

        FileStream s = new FileStream ("async_read.cs",  FileMode.Open);

        s.Position = 0;
        sum0 = 0;
        while (s.Read (buf, 0, 1) == 1)
            sum0 += buf [0];

        s.Position = 0;

        do {
            ar = s.BeginRead (buf, 0, 1, ac, buf);
        } while (s.EndRead (ar) == 1);
        sum -= buf [0];

        Thread.Sleep (100);

        s.Close ();

        Console.WriteLine ("CSUM: " + sum + " " + sum0);
        if (sum != sum0)
            return 1;

        return 0;
    }
开发者ID:robertmichaelwalsh,项目名称:CSharpFrontEnd,代码行数:31,代码来源:async_read.cs

示例2: Slice

    static void Slice(string sourceFile, string destinationDirectory, int parts)
    {
        byte[] buffer = new byte[4096];
        using (FileStream readingStream = new FileStream(sourceFile, FileMode.Open))
        {
            long size = readingStream.Length / 4096;
            size = size / parts;
            int readBytes;
            string fileExtension = readingStream.Name.Substring(readingStream.Name.Length - 4);

            for (int i = 0; i < parts -1; i++)
            {
                using (FileStream writingStream = new FileStream(destinationDirectory + "\\Part-" + i + fileExtension, FileMode.Create))
                {
                    for (int j = 0; j < size; j++)
                    {
                        readBytes = readingStream.Read(buffer, 0, buffer.Length);
                        writingStream.Write(buffer, 0, readBytes);

                    }
                }
            }
            using (FileStream writingStream = new FileStream(destinationDirectory + "\\Part-" + (parts - 1) + fileExtension, FileMode.Create))
            {
                while ((readBytes = readingStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    writingStream.Write(buffer, 0, readBytes);
                }
            }
        }
    }
开发者ID:psdimitrov,项目名称:homeworks,代码行数:31,代码来源:SlicingFiles.cs

示例3: LoadName

	public static string LoadName(string path)
	{
		// The idea is to read only the number of bytes we need to determine the name of the timeline
		// We're going to exploit our knowledge of the way the unpacker works to do this - kinda hacky, but eh
		// Maybe one of these days I'll change the unpacker over to work with streams instead of byte arrays
		// Probably not
		// ...definitely not
		FileStream fs = new FileStream(path, FileMode.Open);

		// Skip over the version
		fs.Position = 4;	

		// Read the length of the string
		byte[] strLengthBytes = new byte[4];
		fs.Read(strLengthBytes, 0, 4);
		int strLength = BitConverter.ToInt32(strLengthBytes, 0);

		// Read the actual string data
		byte[] strBytes = new byte[strLength];
		fs.Read(strBytes, 0, strLength);

		// Tidy up the stream
		fs.Close();

		// Unpack and return the string
        char[] cArray = System.Text.Encoding.Unicode.GetChars(strBytes, 0, strBytes.Length);
        return new string(cArray);
	}
开发者ID:datalurkur,项目名称:HAM,代码行数:28,代码来源:HamTimeline.cs

示例4: Slice

    static void Slice(string sourceFile, string destinationDirectory, int parts)
    {
        using (var file = new FileStream(sourceFile, FileMode.Open))
        {
            Regex regex = new Regex(@"\.(?<=\.)(\w+)");
            type = Convert.ToString(regex.Match(sourceFile));

            long PartLength = file.Length / parts + 1;
            byte[] part = new byte[file.Length * 2];

            int start = 0;

            int partName = 0;
            while (true)
            {
                int readBytes = file.Read(part, start, (int)PartLength);    // тук ще счупи
                if (readBytes == 0)
                {
                    break;
                }

                string resultFileName = destinationDirectory + "Part-" + ++partName + type;
                resultNames.Add(resultFileName);
                using (var filePart = new FileStream(resultFileName, FileMode.Create))
                {
                    filePart.Write(part, start, readBytes);
                }
                start += (int)PartLength;                                   // тук ще счупи
            }
        }
    }
开发者ID:teodory,项目名称:SoftUni-Homeworks,代码行数:31,代码来源:Problem5.cs

示例5: Assemble

 public static void Assemble(List<string> files, string outputDir)
 {
     string fileExt = Path.GetExtension(files[0]);
     Directory.CreateDirectory(outputDir);
     string outputFile = string.Format(@"{0}\assembled_file{1}", outputDir, fileExt);
     using (var writer = new FileStream(outputFile, FileMode.Create))
     {
         for (int i = 0; i < files.Count; i++)
         {
             using (var reader = new FileStream(files[i], FileMode.Open))
             {
                 byte[] buffer = new byte[4096];
                 while (true)
                 {
                     int readBytes = reader.Read(buffer, 0, buffer.Length);
                     if (readBytes == 0)
                     {
                         break;
                     }
                     writer.Write(buffer, 0, readBytes);
                 }
             }
         }
     }
 }
开发者ID:KostaKanev,项目名称:myCodes,代码行数:25,代码来源:SlicingFiles.cs

示例6: Compute

    static IEnumerator Compute(string path, OnFinish on_finish)
    {
        FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        byte[] buffer = new byte[kBlockSize];
        MD5 md5 = MD5.Create();
        int length, read_bytes;

        while (stream.Position < stream.Length)
        {
            length = (stream.Position + kBlockSize > stream.Length) ? (int)(stream.Length - stream.Position) : kBlockSize;
            read_bytes = stream.Read(buffer, 0, length);

            if (stream.Position < stream.Length)
            {
                md5.TransformBlock(buffer, 0, read_bytes, buffer, 0);
            }
            else
            {
                md5.TransformFinalBlock(buffer, 0, read_bytes);
                stream.Close();
                break;
            }

            yield return new WaitForEndOfFrame();
        }

        string md5hash = "";
        foreach (byte n in md5.Hash)
            md5hash += n.ToString("x2");

        Fun.DebugUtils.Log(String.Format("MD5 >> {0} > {1}", stream.Name, md5hash));

        if (on_finish != null)
            on_finish(md5hash);
    }
开发者ID:tixiah,项目名称:engine-plugin-unity3d,代码行数:35,代码来源:MD5Async.cs

示例7: Assemble

    private static void Assemble(List<string> files, string destinationDirectory)
    {
        string fileOutputPath = destinationDirectory + "assembled" + "." + matches[0].Groups[2];
        var fsSource = new FileStream(fileOutputPath, FileMode.Create);

        fsSource.Close();
        using (fsSource = new FileStream(fileOutputPath, FileMode.Append))
        {
            foreach (var filePart in files)
            {
                using (var partSource = new FileStream(filePart, FileMode.Open))
                {
                    Byte[] bytePart = new byte[4096];
                    while (true)
                    {
                        int readBytes = partSource.Read(bytePart, 0, bytePart.Length);
                        if (readBytes == 0)
                        {
                            break;
                        }

                        fsSource.Write(bytePart, 0, readBytes);
                    }
                }
            }
        }
    }
开发者ID:zhecho1215,项目名称:HomeWorks,代码行数:27,代码来源:SlicingFile.cs

示例8: btnSubmit_Click

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime dealStartDate = new DateTime(int.Parse(ddlStartYear.SelectedValue), int.Parse(ddlStartMonth.SelectedValue), int.Parse(ddlStartDay.SelectedValue));
        DateTime dealEndDate = new DateTime(int.Parse(ddlEndYear.SelectedValue), int.Parse(ddlEndMonth.SelectedValue), int.Parse(ddlEndDay.SelectedValue));
        if (dealStartDate <= dealEndDate)
        {
            string[] imageExtensions = {".JPEG",".JPG",".TIFF",".GIF",".BMP",".PNG" };
        if (DealPictureUploader.HasFile)
        {
            string fileExtension = Path.GetExtension(DealPictureUploader.FileName).ToUpper();
            if (imageExtensions.Contains(fileExtension.ToUpper()))
            {
            //string imageUrl = Path.GetFullPath(DealPictureUploader.FileName);

            FileStream fs = new FileStream(@DealPictureUploader.FileName, FileMode.Open, FileAccess.Read);
            byte[] dealImageData = new byte[fs.Length];
            fs.Read(dealImageData, 0, Convert.ToInt32(fs.Length));
            string dealTitle = txtTitle.Text.Trim();
            string dealDesc = txtDescription.Text.Trim();
            decimal dealPrice = decimal.Parse(txtPrice.Text.Trim());
            int dealCategory = int.Parse(ddlCategory.SelectedValue);
            string dealUrl = txtDealUrl.Text.Trim();
            lblResult.Text = _dBal.ShareTheDeal(dealTitle, dealDesc, dealPrice, dealCategory, dealUrl, dealImageData,lblUserName.Text,dealStartDate,dealEndDate);
            }
        }
        }
        else
        {
            lblResult.Text = "Deal start date should not be < Deal end date";
        }
    }
开发者ID:rammohanravi,项目名称:IHD,代码行数:31,代码来源:ShareDeal.aspx.cs

示例9: Slice

    static void Slice(string sourceFile, string destinationDirectory, int parts)
    {
        string extension = new FileInfo(sourceFile).Extension;
        ext = extension;

        using (var source = new FileStream(destinationDirectory + sourceFile, FileMode.Open))
        {
            int pieceSize = (int)Math.Ceiling((decimal)source.Length / parts);
            byte[] buffer = new byte[4096];

            for (int i = 1; i <= parts; i++)
            {

                string currentFilePath = string.Format(destinationDirectory + "part" + i + extension);
                using (var destination =
                    new FileStream(currentFilePath, FileMode.Create))
                {

                    long partBytes = 0;
                    while (partBytes < pieceSize)
                    {
                        int readBytes = source.Read(buffer, 0, buffer.Length);
                        if (readBytes == 0)
                        {
                            break;;
                        }
                        destination.Write(buffer, 0, readBytes);
                        partBytes += readBytes;
                    }
                }
                slicedFiles.Add(currentFilePath);
            }
        }
    }
开发者ID:BiserVStoev,项目名称:SoftUni-CSharp-Advanced,代码行数:34,代码来源:SlicingFile.cs

示例10: Slice

 static void Slice(string sourceFile, string destinationDirectory, int parts)
 {
     byte[] buffer = new byte[4096];
     long fileSize = new FileInfo(sourceFile).Length;
     string extension = new FileInfo(sourceFile).Extension;
     ext = extension;
     int pieceSize = (int)Math.Ceiling((decimal)fileSize / parts);
     using (FileStream reader = new FileStream(sourceFile, FileMode.Open))
     {
         for (int part = 0; part < parts; part++)
         {
             string destinationFileName = string.Format("Part-{0}", (part + 1).ToString("D3")) + extension;
             using (FileStream writer = new FileStream(destinationFileName, FileMode.Append))
             {
                 long currentPieceSize = 0;
                 while (currentPieceSize < pieceSize)
                 {
                     // read piece
                     int bytesRead = reader.Read(buffer, 0, buffer.Length);
                     if (bytesRead == 0) break;
                     // write piece
                     writer.Write(buffer, 0, bytesRead);
                     currentPieceSize += bytesRead;
                 }
             }
         }
     }
 }
开发者ID:AsenTahchiyski,项目名称:SoftUni,代码行数:28,代码来源:SlicingFile.cs

示例11: Main

    static void Main()
    {
        string filePath = "../../dog.jpg";
        string copyPath = "../../copy.txt";
        FileStream file = new FileStream(filePath, FileMode.Open);
        FileStream copy = new FileStream(copyPath, FileMode.Open);

        using (file)
        {
            using (copy)
            {
                byte[] buffer = new byte[4096];
                while (true)
                {
                    int readBytes = file.Read(buffer, 0, buffer.Length);
                    if (readBytes == 0)
                    {
                        break;
                    }
                    else
                    {
                        copy.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }
开发者ID:ZahariyaPehlivanova,项目名称:CSharpAdvanced-Homework-StreamsAndFiles,代码行数:27,代码来源:Program.cs

示例12: Main

	static int Main () {
		byte [] buf = new byte [1];
		AsyncCallback ac = new AsyncCallback (async_callback);
		IAsyncResult ar;
		int sum0 = 0;
		int count0 = 0;
		
		FileStream s = new FileStream ("async_read.exe",  FileMode.Open, FileAccess.Read);

		s.Position = 0;
		while (s.Read (buf, 0, 1) == 1) {
			sum0 += buf [0];
			count0 ++;
		}
		
		s.Position = 0;
		
		do {
			buf = new byte [1];
			ar = s.BeginRead (buf, 0, 1, ac, buf);
		} while (s.EndRead (ar) == 1);
		
		Thread.Sleep (100);
		
		s.Close ();

		count0 ++;  // async_callback is invoked for the "finished reading" case too
		Console.WriteLine ("CSUM: " + sum + " " + sum0);
		Console.WriteLine ("Count: " + count + " " + count0);
		if (sum != sum0 || count != count0)
			return 1;
		
		return 0;
	}
开发者ID:ItsVeryWindy,项目名称:mono,代码行数:34,代码来源:async_read.cs

示例13: Main

    static void Main()
    {

        string NImagePath = "../../image.jpg";
        string DestinationPath = "../../copy.jpg";


        using (var source = new FileStream(NImagePath, FileMode.Open))
        {
            using (var destination = new FileStream(DestinationPath, FileMode.Create))
            {
                byte[] buffer = new byte[source.Length];
                while (true)
                {
                    int readBytes = source.Read(buffer, 0, buffer.Length);
                    if (readBytes == 0)
                    {
                        break;
                    }


                    destination.Write(buffer, 0, readBytes);
                }
            }
        }
    }
开发者ID:zhecho1215,项目名称:HomeWorks,代码行数:26,代码来源:CopyBinaryFile.cs

示例14: Main

        static void Main(string[] args)
        {
            FileStream inputFile = new FileStream("C:\\filename.exe", FileMode.Open);
            byte[] byteBuffer = new byte[inputFile.Length];
            inputFile.Read(byteBuffer, 0, (int)inputFile.Length);

            int iColCount = 0;

            for (int i = 0; i < byteBuffer.Length; i++)
            {
                iColCount++;

                string sHex = byteBuffer[i].ToString("X2");
                Console.Write(sHex + " ");

                if (iColCount == 20)
                {
                    iColCount = 0;
                    Console.WriteLine();
                }
            }

            // Wait 
            Console.Read(); 

        }
开发者ID:caiobarretta,项目名称:ConsoleApplication1,代码行数:26,代码来源:Program.cs

示例15: download_Click

 protected void download_Click(object sender, EventArgs e)
 {
     try { strIdentify = Session["isLogin"].ToString(); }
     catch (Exception ex)
     {
         if(strIdentify!="identified ")
             Response.Redirect("/subject/edit/LoginFail.html");
     };
     string truePath = fullPath + "templates/index.tpl";
     if (File.Exists(truePath))
     {
         try
         {
             FileStream fileStream = new FileStream(truePath, FileMode.Open);
             long fileSize = fileStream.Length;
             Response.Clear();
             Response.ClearHeaders();
             Response.ContentType = "application/octet-stream";
             Response.AddHeader("Content-Disposition", "attachment;filename=index.html");
             Response.AppendHeader("Content-Length", fileSize.ToString());
             byte[] fileBuffer = new byte[fileSize];
             fileStream.Read(fileBuffer, 0, (int)fileSize);
             Response.BinaryWrite(fileBuffer);
             fileStream.Close();
             Response.End();
         }
         catch (Exception ex){
             Response.Write("<script>alert('发生致命错误,详情:"+ex+"');</script>");
         }
     }
     else {
         Response.Write("<script>alert('文件不存在或未生成tpl文件!');window.location.href=document.URL;</script>");
     }
 }
开发者ID:jamieTsang,项目名称:spe,代码行数:34,代码来源:Editor.aspx.cs


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