本文整理汇总了C#中System.IO.FileStream.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.Flush方法的具体用法?C# FileStream.Flush怎么用?C# FileStream.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.Flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddSecretKeytoFile
//Đưa SecretKey vào File của thuật toán 3DES
public void AddSecretKeytoFile(string inputFile)
{
//Add SecretKey to File;
if (File.Exists(inputFile))
{
FileStream fsOpen = new FileStream(inputFile, FileMode.Open, FileAccess.ReadWrite);
try
{
byte[] content = new byte[fsOpen.Length];
fsOpen.Read(content, 0, content.Length);
//string sContent = System.Text.Encoding.UTF8.GetString(content); //noi dung bang string
//byte[] plainbytesCheck = System.Text.Encoding.UTF8.GetBytes(sContent);
byte[] plainbytesKey = new UTF8Encoding(true).GetBytes(m_EncryptedSecretKey + "\r\n");
fsOpen.Seek(0, SeekOrigin.Begin);
fsOpen.Write(plainbytesKey, 0, plainbytesKey.Length);
fsOpen.Flush();
fsOpen.Write(content, 0, content.Length);
fsOpen.Flush();
fsOpen.Close();
}
catch
{
fsOpen.Close();
}
}
}
示例2: DecompressFileLZMA
public static void DecompressFileLZMA(string inFile, string outFile)
{
SevenZip.Sdk.Compression.Lzma.Decoder coder = new SevenZip.Sdk.Compression.Lzma.Decoder();
FileStream input = new FileStream(inFile, FileMode.Open);
FileStream output = new FileStream(outFile, FileMode.Create);
// Read the decoder properties
byte[] properties = new byte[5];
input.Read(properties, 0, 5);
// Read in the decompress file size.
byte[] fileLengthBytes = new byte[8];
input.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
coder.SetDecoderProperties(properties);
coder.Code(input, output, input.Length, fileLength, null);
output.Flush();
output.Close();
try
{
output.Flush();
output.Close();
input.Flush();
input.Close();
}
catch
{
}
}
示例3: Execute
public double Execute(DirectoryInfo targetDirectory, int sizeHintInMb)
{
double result = 0.0;
var file = GetBenchmarkFile(targetDirectory);
if (file.Exists)
{
file.Delete();
}
using (var fileStream = new FileStream(file.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
var sizeInBytes = (long) sizeHintInMb*1000L*1000L;
OnPrepare(fileStream, sizeInBytes);
fileStream.Flush();
fileStream.Seek(0L, SeekOrigin.Begin);
var stopwatch = new Stopwatch();
stopwatch.Start();
var actualSizeInBytes = OnExecute(fileStream,sizeInBytes);
fileStream.Flush(true);
stopwatch.Stop();
result = ((double) actualSizeInBytes/stopwatch.Elapsed.TotalSeconds)/(1000.0*1000.0);
}
if (file.Exists)
{
file.Delete();
}
return result;
}
示例4: FlushSetLengthAtEndOfBuffer
public void FlushSetLengthAtEndOfBuffer()
{
// This is a regression test for a bug with FileStream’s Flush()
// and SetLength() methods.
// The read-buffer was not flushed inside Flush and SetLength when
// the buffer pointer was at the end. This causes subsequent Seek
// and Read calls to operate on stale/wrong data.
// customer reported repro
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.SetLength(200);
fs.Flush();
// write 119 bytes starting from Pos = 28
fs.Seek(28, SeekOrigin.Begin);
Byte[] buffer = new Byte[119];
for (int i = 0; i < buffer.Length; i++)
buffer[i] = Byte.MaxValue;
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
// read 317 bytes starting from Pos = 84;
fs.Seek(84, SeekOrigin.Begin);
fs.Read(new byte[1024], 0, 317);
fs.SetLength(135);
fs.Flush();
// read one byte at Pos = 97
fs.Seek(97, SeekOrigin.Begin);
Assert.Equal(fs.ReadByte(), (int)Byte.MaxValue);
}
}
示例5: FlushThrowsForDisposedStream
public void FlushThrowsForDisposedStream()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => fs.Flush(false));
Assert.Throws<ObjectDisposedException>(() => fs.Flush(true));
}
}
示例6: BasicFlushFunctionality
public void BasicFlushFunctionality()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.WriteByte(0);
fs.Flush(false);
fs.WriteByte(0xFF);
fs.Flush(true);
}
}
示例7: CopyFile
/// <summary>
/// 文件的复制
/// </summary>
/// <param FormerFile="string">源文件路径</param>
/// <param toFile="string">目的文件路径</param>
/// <param SectSize="int">传输大小</param>
/// <param progressBar="ProgressBar">ProgressBar控件</param>
public void CopyFile(string FormerFile, string toFile, int SectSize, ProgressBar progressBar1)
{
progressBar1.Value = 0;//设置进度栏的当前位置为0
progressBar1.Minimum = 0;//设置进度栏的最小值为0
FileStream fileToCreate = new FileStream(toFile, FileMode.Create);//创建目的文件,如果已存在将被覆盖
fileToCreate.Close();//关闭所有资源
fileToCreate.Dispose();//释放所有资源
FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件
ToFileOpen = new FileStream(toFile, FileMode.Append, FileAccess.Write);//以写方式打开目的文件
int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));//根据一次传输的大小,计算传输的个数
progressBar1.Maximum = max;//设置进度栏的最大值
int FileSize;//要拷贝的文件的大小
if (SectSize < FormerOpen.Length)//如果分段拷贝,即每次拷贝内容小于文件总长度
{
byte[] buffer = new byte[SectSize];//根据传输的大小,定义一个字节数组
int copied = 0;//记录传输的大小
int tem_n = 1;//设置进度栏中进度块的增加个数
while (copied <= ((int)FormerOpen.Length - SectSize))//拷贝主体部分
{
FileSize = FormerOpen.Read(buffer, 0, SectSize);//从0开始读,每次最大读SectSize
FormerOpen.Flush();//清空缓存
ToFileOpen.Write(buffer, 0, SectSize);//向目的文件写入字节
ToFileOpen.Flush();//清空缓存
ToFileOpen.Position = FormerOpen.Position;//使源文件和目的文件流的位置相同
copied += FileSize;//记录已拷贝的大小
progressBar1.Value = progressBar1.Value + tem_n;//增加进度栏的进度块
}
int left = (int)FormerOpen.Length - copied;//获取剩余大小
FileSize = FormerOpen.Read(buffer, 0, left);//读取剩余的字节
FormerOpen.Flush();//清空缓存
ToFileOpen.Write(buffer, 0, left);//写入剩余的部分
ToFileOpen.Flush();//清空缓存
}
else//如果整体拷贝,即每次拷贝内容大于文件总长度
{
byte[] buffer = new byte[FormerOpen.Length];//获取文件的大小
FormerOpen.Read(buffer, 0, (int)FormerOpen.Length);//读取源文件的字节
FormerOpen.Flush();//清空缓存
ToFileOpen.Write(buffer, 0, (int)FormerOpen.Length);//写放字节
ToFileOpen.Flush();//清空缓存
}
FormerOpen.Close();//释放所有资源
ToFileOpen.Close();//释放所有资源
if (MessageBox.Show("复制完成") == DialogResult.OK)//显示"复制完成"提示对话框
{
progressBar1.Value = 0;//设置进度栏的当有位置为0
textBox1.Clear();//清空文本
textBox2.Clear();
str = "";
}
}
示例8: FlushOnReadOnlyFileDoesNotThrow
public void FlushOnReadOnlyFileDoesNotThrow()
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.WriteByte(0);
}
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
fs.Flush(false);
fs.Flush(true);
}
}
示例9: RenderBody
/// <summary>Renders the body of the rule file in pseudo-code HTML.</summary>
/// <param name="fileName">Output file name to generate.</param>
/// <param name="title">Title for the HTML page.
/// If null, the default value defined in pseudocode_body.xsl will be used.</param>
public void RenderBody(string fileName, string title)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
RenderBody(fs, title);
fs.Flush();
fs.Close();
}
示例10: Run
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Shapes();
// Load the PPTX to Presentation object
Presentation pres = new Presentation(dataDir + "AccessingOLEObjectFrame.pptx");
// Access the first slide
ISlide sld = pres.Slides[0];
// Cast the shape to OleObjectFrame
OleObjectFrame oof = (OleObjectFrame)sld.Shapes[0];
// Read the OLE Object and write it to disk
if (oof != null)
{
FileStream fstr = new FileStream(dataDir+ "excelFromOLE_out.xlsx", FileMode.Create, FileAccess.Write);
byte[] buf = oof.ObjectData;
fstr.Write(buf, 0, buf.Length);
fstr.Flush();
fstr.Close();
}
}
示例11: SaveBatches
/// <summary>
/// Save batches and the header.
/// </summary>
/// <param name="batchList">BatchList to save</param>
/// <param name="header">Header information</param>
public void SaveBatches(List<Batch> batchList, BatchHeader header)
{
// Create the batch directory.
var path = Path.Combine(_batchSpoolDirectory, header.BatchCode.ToString());
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
foreach (var b in batchList)
{
string filePath = Path.Combine(path, b.FileName);
var formatter = new BinaryFormatter();
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
{
formatter.Serialize(fileStream, b);
fileStream.Flush();
}
}
string headerFilePath = Path.Combine(path, BATCH_HEADER_FILENAME);
var headerFormatter = new BinaryFormatter();
using (var fileStream = new FileStream(headerFilePath, FileMode.Create, FileAccess.ReadWrite))
{
headerFormatter.Serialize(fileStream, header);
fileStream.Flush();
}
}
示例12: TestWrite
private bool TestWrite(FileStream fs, int BufferLength, int BytesToWrite, int Offset)
{
bool result = true;
long startLength = fs.Position;
long nextbyte = startLength % 256;
byte[] byteBuffer = new byte[BufferLength];
for (int i = Offset; i < (Offset + BytesToWrite); i++)
{
byteBuffer[i] = (byte)nextbyte;
// Reset if wraps past 255
if (++nextbyte > 255)
nextbyte = 0;
}
fs.Write(byteBuffer, Offset, BytesToWrite);
fs.Flush();
if ((startLength + BytesToWrite) < fs.Length)
{
result = false;
Log.Exception("Expeceted final length of " + (startLength + BytesToWrite) + " bytes, but got " + fs.Length + " bytes");
}
return result;
}
示例13: Init
public void Init() {
_sign=Topic.root.Get("/etc/PersistentStorage");
_verbose=_sign.Get("verbose");
_verbose.config=true;
if(!Directory.Exists("../data")) {
Directory.CreateDirectory("../data");
}
_file=new FileStream("../data/persist.xdb", FileMode.OpenOrCreate, FileAccess.ReadWrite);
if(_file.Length<=0x40) {
_file.Write(new byte[0x40], 0, 0x40);
_file.Flush(true);
_nextBak=DateTime.Now.AddHours(1);
} else {
Load();
}
_fileLength=_file.Length;
_work=new AutoResetEvent(false);
_thread=new Thread(new ThreadStart(PrThread));
_thread.Priority=ThreadPriority.BelowNormal;
_now=DateTime.Now;
if(_nextBak<_now) {
Backup();
}
_thread.Start();
Topic.root.all.changed+=MqChanged;
}
示例14: DoDownload
private void DoDownload(string source, string output)
{
WebRequest req = WebRequest.Create(source);
WebResponse rsp = req.GetResponse();
Stream stream = rsp.GetResponseStream();
string path = Path.GetDirectoryName(output);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
using (var fs = new FileStream(output, FileMode.Create))
{
int readCount = -1;
var buffer = new byte[1024*64];
while (readCount != 0)
{
readCount = stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, readCount);
}
fs.Flush();
}
_logger.InfoLine(source + " ==> " + output);
}
示例15: TestGetPolicySetBytes
public void TestGetPolicySetBytes()
{
string testFilename = Path.Combine(m_testPath, "Has everthing.policy");
string outputFilename = Path.Combine(m_testPath, "TestGetPolicySetBytes.policy");
try
{
PolicySetItem policySetItem = ReadPolicySet(testFilename);
IPolicySet policySet = policySetItem.Data;
byte[] rhsBytes = PolicySuites.Instance.GetPolicySetBytes(policySet, SaveOption.SaveOnly);
Assert.IsTrue(0 < rhsBytes.Length, "Expected the rhs to contain bytes after a read");
using(FileStream outStream = new FileStream(outputFilename, FileMode.Create))
{
outStream.Write(rhsBytes, 0, (int)rhsBytes.Length);
outStream.Flush();
}
Assert.IsTrue(File.Exists(outputFilename));
PolicySetItem resultantPolicySetItem = ReadPolicySet(outputFilename);
IPolicySet resultantPolicySet = resultantPolicySetItem.Data;
Assert.IsNotNull(resultantPolicySet);
AreEquivalent(policySetItem, resultantPolicySetItem);
}
finally
{
if (File.Exists(outputFilename))
File.Delete(outputFilename);
}
}