C# FileStream.Write的代码示例
通过代码示例来学习C# FileStream.Write方法
通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。
FileStream.Write是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的FileStream.Write() 已经帮大家高亮显示了,大家可以重点学习FileStream.Write() 方法的写法,从而快速掌握该方法的应用。
FileStream.Write的代码示例1 - IniCheckPermissions()
using System.IO; bool IniCheckPermissions() { bool deleteAfter = !File.Exists(IniFilename); bool hasPermission = false; try { using (FileStream fileStream = File.Open(IniFilename, FileMode.OpenOrCreate, FileAccess.Write)) { hasPermission = true; } } catch { hasPermission = false; } if (File.Exists(IniFilename) && deleteAfter) try { File.Delete(IniFilename); } catch { } return hasPermission; }
开发者ID:emoose,项目名称:DLSSTweaks,代码行数:28,代码来源:Main.cs
在IniCheckPermissions()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例2 - WriteToFileOrStream()
using System.IO; #endregion #region Private Method private void WriteToFileOrStream() { if( _filename != null && !_isCopyingDocument) { using( var fs = new FileStream( _filename, FileMode.Create ) ) { if( _memoryStream.CanSeek ) { // Write to the beginning of the stream _memoryStream.Position = 0; HelperFunctions.CopyStream( _memoryStream, fs ); } else fs.Write( _memoryStream.ToArray(), 0, (int)_memoryStream.Length ); } } else if( _stream.CanSeek ) //Check if stream can be seeked to support System.Web.HttpResponseStream. { // Set the length of this stream to 0 _stream.SetLength( 0 ); // Write to the beginning of the stream _stream.Position = 0; _memoryStream.WriteTo( _stream ); _memoryStream.Flush(); } }
开发者ID:xceedsoftware,项目名称:DocX,代码行数:91,代码来源:DocX.cs
在WriteToFileOrStream()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例3 - FilesAreBufferedAndCanBeFlushed()
using System.IO; [TestCaseSource(typeof(FileRunnersAndFolders), nameof(FileRunnersAndFolders.Runners))] public void FilesAreBufferedAndCanBeFlushed(FileSystemRunner fileSystem, string parentFolder) { string filename = Path.Combine(parentFolder, "FilesAreBufferedAndCanBeFlushed"); string filePath = this.Enlistment.GetVirtualPathTo(filename); byte[] buffer = System.Text.Encoding.ASCII.GetBytes("Some test data"); using (FileStream writeStream = File.Open(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { writeStream.Write(buffer, 0, buffer.Length); using (FileStream readStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { readStream.Length.ShouldEqual(0); writeStream.Flush(); readStream.Length.ShouldEqual(buffer.Length); byte[] readBuffer = new byte[buffer.Length]; readStream.Read(readBuffer, 0, readBuffer.Length).ShouldEqual(readBuffer.Length); readBuffer.ShouldMatchInOrder(buffer); } } fileSystem.DeleteFile(filePath); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:28,代码来源:BasicFileSystemTests.cs
在FilesAreBufferedAndCanBeFlushed()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例4 - CreateGitScript()
using System.IO; // TODO(#1364): Don't call this method on POSIX platforms (or have it no-op on them) private void CreateGitScript(GVFSEnlistment enlistment) { FileInfo gitCmd = new FileInfo(Path.Combine(enlistment.EnlistmentRoot, "git.cmd")); using (FileStream fs = gitCmd.Create()) using (StreamWriter writer = new StreamWriter(fs)) { writer.Write( @" @echo OFF echo . echo ^[105;30m echo This repo was cloned using GVFS, and the git repo is in the 'src' directory echo Switching you to the 'src' directory and rerunning your git command echo [0m @echo ON cd src git %* "); } gitCmd.Attributes = FileAttributes.Hidden; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:26,代码来源:CloneVerb.cs
在CreateGitScript()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例5 - OpenFileThenCheckout()
using System.IO; [TestCase] public void OpenFileThenCheckout() { string virtualFile = Path.Combine(this.Enlistment.RepoRoot, GitCommandsTests.EditFilePath); string controlFile = Path.Combine(this.ControlGitRepo.RootPath, GitCommandsTests.EditFilePath); // Open files with ReadWrite sharing because depending on the state of the index (and the mtimes), git might need to read the file // as part of status (while we have the handle open). using (FileStream virtualFS = File.Open(virtualFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) using (StreamWriter virtualWriter = new StreamWriter(virtualFS)) using (FileStream controlFS = File.Open(controlFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) using (StreamWriter controlWriter = new StreamWriter(controlFS)) { this.ValidateGitCommand("checkout -b tests/functional/OpenFileThenCheckout"); virtualWriter.WriteLine("// Adding a line for testing purposes"); controlWriter.WriteLine("// Adding a line for testing purposes"); this.ValidateGitCommand("status"); } // NOTE: Due to optimizations in checkout -b, the modified files will not be included as part of the // success message. Validate that the succcess messages match, and the call to validate "status" below // will ensure that GVFS is still reporting the edited file as modified. string controlRepoRoot = this.ControlGitRepo.RootPath; string gvfsRepoRoot = this.Enlistment.RepoRoot; string command = "checkout -b tests/functional/OpenFileThenCheckout_2"; ProcessResult expectedResult = GitProcess.InvokeProcess(controlRepoRoot, command); ProcessResult actualResult = GitHelpers.InvokeGitAgainstGVFSRepo(gvfsRepoRoot, command); GitHelpers.ErrorsShouldMatch(command, expectedResult, actualResult); actualResult.Errors.ShouldContain("Switched to a new branch"); this.ValidateGitCommand("status"); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:35,代码来源:GitCommandsTests.cs
在OpenFileThenCheckout()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了2次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例6 - WriteAllEntries()
using System.IO; private void WriteAllEntries(uint version, bool isFinal) { try { using (Stream indexStream = new FileStream(this.indexLockPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (BinaryWriter writer = new BinaryWriter(indexStream)) { writer.Write(IndexHeader); writer.Write(EndianHelper.Swap(version)); writer.Write((uint)0); // Number of entries placeholder uint lastStringLength = 0; LsTreeEntry entry; while (this.entryQueue.TryTake(out entry, Timeout.Infinite)) { this.WriteEntry(writer, version, entry.Sha, entry.Filename, ref lastStringLength); } // Update entry count writer.BaseStream.Position = EntryCountOffset; writer.Write(EndianHelper.Swap(this.entryCount)); writer.Flush(); } this.AppendIndexSha(); if (isFinal) { this.ReplaceExistingIndex(); } } catch (Exception e) { this.tracer.RelatedError("Failed to generate index: {0}", e.ToString()); this.HasFailures = true; } }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:38,代码来源:GitIndexGenerator.cs
在WriteAllEntries()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了5次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例7 - TryWriteTempFile()
using System.IO; public virtual bool TryWriteTempFile( ITracer tracer, Stream source, string tempFilePath, out long fileLength, out Task flushTask, bool throwOnError = false) { fileLength = 0; flushTask = null; try { Stream fileStream = null; try { fileStream = this.fileSystem.OpenFileStream( tempFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, callFlushFileBuffers: false); // Any flushing to disk will be done asynchronously StreamUtil.CopyToWithBuffer(source, fileStream); fileLength = fileStream.Length; if (this.Enlistment.FlushFileBuffersForPacks) { // Flush any data buffered in FileStream to the file system fileStream.Flush(); // FlushFileBuffers using FlushAsync // Do this last to ensure that the stream is not being accessed after it's been disposed flushTask = fileStream.FlushAsync().ContinueWith((result) => fileStream.Dispose()); } } finally { if (flushTask == null && fileStream != null) { fileStream.Dispose(); } } this.ValidateTempFile(tempFilePath, tempFilePath); } catch (Exception ex) { if (flushTask != null) { flushTask.Wait(); flushTask = null; } this.CleanupTempFile(this.Tracer, tempFilePath); if (tracer != null) { EventMetadata metadata = CreateEventMetadata(ex); metadata.Add("tempFilePath", tempFilePath); tracer.RelatedWarning(metadata, $"{nameof(this.TryWriteTempFile)}: Exception caught while writing temp file", Keywords.Telemetry); } if (throwOnError) { throw; } else { return false; } } return true; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:77,代码来源:GitObjects.cs
在TryWriteTempFile()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
FileStream.Write的代码示例8 - SignIndex()
using System.IO; private void SignIndex() { using (ITracer activity = this.tracer.StartActivity("SignIndex", EventLevel.Informational, Keywords.Telemetry, metadata: null)) { using (FileStream fs = File.Open(this.indexPath, FileMode.Open, FileAccess.ReadWrite)) { // Truncate the old hash off. The Index class is expected to preserve any existing hash. fs.SetLength(fs.Length - 20); using (HashingStream hashStream = new HashingStream(fs)) { fs.Position = 0; hashStream.CopyTo(Stream.Null); byte[] hash = hashStream.Hash; // The fs pointer is now where the old hash used to be. Perfect. :) fs.Write(hash, 0, hash.Length); } } } }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:22,代码来源:Index.cs
在SignIndex()方法中,FileStream的代码示例类中的Write的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
本文中的FileStream.Write方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。