本文整理汇总了C#中FileInfo.Write方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.Write方法的具体用法?C# FileInfo.Write怎么用?C# FileInfo.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileInfo
的用法示例。
在下文中一共展示了FileInfo.Write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteTimeRefreshes
public void WriteTimeRefreshes()
{
string fileName = GetTestFilePath();
File.Create(fileName).Dispose();
FileInfo fileInfo = new FileInfo(fileName);
DateTime beforeWrite = DateTime.Now.AddSeconds(-1);
using (Stream stream = new FileInfo(fileName).OpenWrite())
{
stream.Write(new Byte[] { 10 }, 0, 1);
}
DateTime afterWrite = DateTime.Now;
fileInfo.Refresh();
DateTime lastWriteTime = fileInfo.LastWriteTime;
Assert.InRange<long>(fileInfo.LastWriteTime.Ticks, beforeWrite.Ticks, afterWrite.Ticks);
//Read from the File and test the writeTime to ensure it is unchanged
using (Stream stream = new FileInfo(fileName).OpenRead())
{
stream.Read(new Byte[1], 0, 1);
}
Assert.Equal(fileInfo.LastWriteTime, lastWriteTime);
//Write to the file again to test lastWriteTime can be updated again
beforeWrite = DateTime.Now.AddSeconds(-1);
using (Stream stream = fileInfo.Open(FileMode.Open))
{
stream.Write(new Byte[] { 10 }, 0, 1);
}
afterWrite = DateTime.Now;
Assert.Equal(fileInfo.LastWriteTime, lastWriteTime); //needs to be refreshed first
fileInfo.Refresh();
Assert.InRange<long>(fileInfo.LastWriteTime.Ticks, beforeWrite.Ticks, afterWrite.Ticks);
}
示例2: Write
protected override void Write(string path, string content)
{
var writer = new FileInfo(path).AppendText();
writer.Write(content);
writer.Dispose();
}
示例3: WriteFile
private void WriteFile(StringBuilder fileContents, string fileName)
{
string mwlDirectory = ConfigurationManager.AppSettings["MWLDirectory"];
string mwlFilePath = mwlDirectory + "\\" + fileName + ".xml";
StreamWriter mwlFile = new FileInfo(mwlFilePath).CreateText();
mwlFile.Write(fileContents.ToString());
mwlFile.Close();
}