本文整理汇总了C#中FileInfo.Refresh方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.Refresh方法的具体用法?C# FileInfo.Refresh怎么用?C# FileInfo.Refresh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileInfo
的用法示例。
在下文中一共展示了FileInfo.Refresh方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: AttributeChange
public void AttributeChange()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
testFile.Attributes = FileAttributes.ReadOnly;
testFile.Refresh();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
testFile.Attributes = new FileAttributes();
testFile.Refresh();
Assert.True((testFile.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly);
}
示例3: IsReadOnly_Set_And_Get
public void IsReadOnly_Set_And_Get()
{
var test = new FileInfo(GetTestFilePath());
test.Create().Dispose();
// Set to True
test.IsReadOnly = true;
test.Refresh();
Assert.Equal(true, test.IsReadOnly);
// Set To False
test.IsReadOnly = false;
test.Refresh();
Assert.Equal(false, test.IsReadOnly);
}
示例4: UnixAttributeSetting
public void UnixAttributeSetting(FileAttributes attr)
{
var test = new FileInfo(GetTestFilePath());
test.Create().Dispose();
Set(test.FullName, attr);
test.Refresh();
Assert.Equal(attr, Get(test.FullName));
Set(test.FullName, 0);
}
示例5: DeleteThenRefresh
public void DeleteThenRefresh()
{
FileInfo testFile = new FileInfo(GetTestFilePath());
testFile.Create().Dispose();
Assert.True(testFile.Exists);
testFile.Delete();
testFile.Refresh();
Assert.False(testFile.Exists);
}
示例6: NameChange
public void NameChange()
{
string source = GetTestFilePath();
string dest = GetTestFilePath();
FileInfo testFile = new FileInfo(source);
testFile.Create().Dispose();
Assert.Equal(source, testFile.FullName);
testFile.MoveTo(dest);
testFile.Refresh();
Assert.Equal(dest, testFile.FullName);
}
示例7: SetFileAttributes
/// <summary>
/// 设置文件为只读和隐藏
/// </summary>
static void SetFileAttributes(FileInfo file)
{
//将文件同时设置为只读、隐藏
FileAttributes hiddenAndReadOnlyAttrs = file.Attributes | FileAttributes.Hidden | FileAttributes.ReadOnly;
File.SetAttributes(file.FullName, hiddenAndReadOnlyAttrs);
//需要刷新,否则下面读取出来的数据有延迟
file.Refresh();
FileAttributes attrs = file.Attributes;
Console.WriteLine("是否只读:{0}", attrs.HasFlag(FileAttributes.ReadOnly));
Console.WriteLine("是否隐藏:{0}", attrs.HasFlag(FileAttributes.Hidden));
Console.WriteLine();
}
示例8: InitializeExistsAfterCreation
public void InitializeExistsAfterCreation()
{
string fileName = GetTestFilePath();
FileInfo di = new FileInfo(fileName);
Assert.False(di.Exists);
File.Create(fileName).Dispose();
// data should be stale
Assert.False(di.Exists);
// force refresh
di.Refresh();
Assert.True(di.Exists);
}
示例9: MakeFile
private static void MakeFile(int x)
{
string line = "This file is full of starts and finishies, replace all the start, if u can; start";
StreamWriter str = new StreamWriter(@"..\..\Files\bigFile.txt");
using (str)
{
FileInfo newFile = new FileInfo(@"..\..\Files\bigFile.txt");
while (newFile.Length <= x)
{
str.WriteLine(line);
newFile.Refresh();
}
}
}
示例10: MakeFile
private static void MakeFile(int x)
{
string line = "this is a very long string";
StreamWriter str = new StreamWriter(@"..\..\Files\bigFile.txt");
using (str)
{
FileInfo newFile = new FileInfo(@"..\..\Files\bigFile.txt");
int index = 0;
while (newFile.Length <= x)
{
str.WriteLine("line {0} : {1}", index + 1, line);
index++;
newFile.Refresh();
}
}
}
示例11: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
String strLoc = "Loc_0001";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
String fileName = s_strTFAbbrev+"TestFile";
FileInfo file2;
Stream stream;
if(File.Exists(fileName))
File.Delete(fileName);
iCountTestcases++;
try {
DateTime dt = File.GetLastWriteTime(null);
iCountErrors++;
printerr( "Error_0002! Expected exception not thrown");
} catch (ArgumentNullException exc){
printinfo("Expected exception thrown :: " + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0003! Unexpected exceptiont thrown: "+exc.ToString());
}
iCountTestcases++;
try {
DateTime dt = File.GetLastWriteTime("");
iCountErrors++;
printerr( "Error_0004! Expected exception not thrown");
} catch (ArgumentException exc){
printinfo("Expected exception thrown :: " + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0005! Unexpected exceptiont thrown: "+exc.ToString());
}
strLoc = "Loc_0006";
file2 = new FileInfo(fileName);
FileStream fs = file2.Create() ;
fs.Write(new Byte[]{10}, 0, 1);
fs.Close();
double lMilliSecs = (DateTime.Now-File.GetLastWriteTime( fileName )).TotalMilliseconds ;
Console.WriteLine("Millis.. " + lMilliSecs );
iCountTestcases++;
Console.WriteLine("FileName ... " + (DateTime.Now - File.GetLastWriteTime( fileName )).Minutes );
Console.WriteLine( DateTime.Now );
Console.WriteLine( File.GetLastWriteTime( fileName ) );
if((DateTime.Now - File.GetLastWriteTime( fileName )).Minutes > 6 ) {
iCountErrors++;
Console.WriteLine((DateTime.Now-File.GetLastWriteTime( fileName )).TotalMilliseconds);
printerr( "Error_0007! Last Write Time time cannot be correct");
}
strLoc = "Loc_0008";
stream = file2.Open(FileMode.Open, FileAccess.Read);
stream.Read(new Byte[1], 0, 1);
stream.Close();
file2.Refresh();
iCountTestcases++;
if((DateTime.Now-File.GetLastWriteTime( fileName )).TotalMinutes >6 ) {
iCountErrors++;
Console.WriteLine((DateTime.Now-File.GetLastWriteTime( fileName )).TotalMilliseconds);
printerr( "Eror_0009! LastWriteTime is way off");
}
file2.Delete();
strLoc = "Loc_0010";
iCountTestcases++;
try {
Console.WriteLine( File.GetLastWriteTime(s_strTFName) );
if( (DateTime.Today - File.GetLastWriteTime(s_strTFName) ).Seconds > 60 ) {
iCountErrors++;
printerr( "Eror_0011! LastWrite time is not correct");
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0012! Unexpected exception thrown: "+exc.ToString());
}
if(File.Exists(fileName))
File.Delete(fileName);
} catch (Exception exc_general ) {
++iCountErrors;
Console.WriteLine (s_strTFAbbrev + " : Error Err_0013! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
}
if ( iCountErrors == 0 )
{
Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
return true;
}
else
{
Console.WriteLine("FAiL! "+s_strTFName+" ,iCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
return false;
}
}
示例12: runTest
public static void runTest()
{
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
FileInfo fil2;
Stream stream;
if (File.Exists(filName))
File.Delete(filName);
// [] Exception for null string
//-----------------------------------------------------------
// [] Create a directory and check the creation time
strLoc = "Loc_r8r7j";
new FileStream(filName, FileMode.Create).Dispose();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(4000).Wait();
fil2.Refresh();
iCountTestcases++;
try
{
// Access time is always shows the midday. For example if you create the file on 3/5/2000.
// The access time should be some thing like... 3/5/2000 12:00:00 AM.
DateTime d1 = DateTime.Now;
DateTime d2 = fil2.LastAccessTime;
if (d1.Year != d2.Year || d1.Month != d2.Month || d1.Month != d2.Month)
{
iCountErrors++;
printerr("Error_20hjx! Creation time cannot be correct");
}
}
catch (Exception exc)
{
iCountErrors++;
printerr("Bug 14952 Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString());
}
// []
strLoc = "Loc_20yxc";
stream = fil2.Open(FileMode.Open);
stream.Read(new Byte[1], 0, 1);
stream.Dispose();
fil2.Refresh();
Task.Delay(1000).Wait();
iCountTestcases++;
try
{
DateTime d1 = DateTime.Now;
DateTime d2 = fil2.LastAccessTime;
if (d1.Year != d2.Year || d1.Month != d2.Month || d1.Month != d2.Month)
{
iCountErrors++;
Console.WriteLine((DateTime.Now - fil2.LastAccessTime).TotalMilliseconds);
printerr("Eror_209x9! LastAccessTime is way off");
}
}
catch (Exception exc)
{
iCountErrors++;
printerr("Error_209jx! Unexpected exception thrown: " + exc.ToString());
}
fil2.Delete();
//-----------------------------------------------------------
if (File.Exists(filName))
File.Delete(filName);
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
//.........这里部分代码省略.........
示例13: runTest
public static void runTest()
{
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
FileInfo fil2;
Stream stream;
if (File.Exists(filName))
File.Delete(filName);
// [] Exception for null string
//-----------------------------------------------------------
// [] Create a directory and check the creation time
strLoc = "Loc_r8r7j";
new FileStream(filName, FileMode.Create).Dispose();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(4000).Wait();
fil2.Refresh();
iCountTestcases++;
DateTime lastWriteTime = fil2.LastWriteTime;
if ((DateTime.Now - lastWriteTime).TotalMilliseconds < 2000 ||
(DateTime.Now - lastWriteTime).TotalMilliseconds > 5000)
{
iCountErrors++;
Console.WriteLine(lastWriteTime);
Console.WriteLine((DateTime.Now - lastWriteTime).TotalMilliseconds);
printerr("Error_20hjx! Last Write Time time cannot be correct");
}
// []
strLoc = "Loc_20yxc";
stream = fil2.Open(FileMode.Open, FileAccess.Read);
stream.Read(new Byte[1], 0, 1);
stream.Dispose();
fil2.Refresh();
if (fil2.LastWriteTime != lastWriteTime)
{
iCountErrors++;
printerr("Eror_209x9! LastWriteTime should not have changed due to a read");
}
stream = fil2.Open(FileMode.Open);
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(3000).Wait();
fil2.Refresh();
iCountTestcases++;
if ((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds < 1000 ||
(DateTime.Now - fil2.LastWriteTime).TotalMilliseconds > 5000)
{
iCountErrors++;
Console.WriteLine((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds);
printerr("Eror_f984f! LastWriteTime is way off");
}
//-----------------------------------------------------------
if (File.Exists(filName))
File.Delete(filName);
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
}
//// Finish Diagnostics
if (iCountErrors != 0)
{
Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString());
}
//.........这里部分代码省略.........
示例14: runTest
public static void runTest()
{
int iCountErrors = 0;
int iCountTestcases = 0;
String strLoc = "Loc_000oo";
String strValue = String.Empty;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
FileStream fs2;
FileInfo fil2;
// COMMENT: Simple string parsing of existing fullpath
// [] Delete a file that does not exist should just return
//-----------------------------------------------------------------
strLoc = "Loc_7198c";
iCountTestcases++;
fil2 = new FileInfo("AkkarBurger");
fil2.Delete();
//-----------------------------------------------------------------
if (Interop.IsWindows) // deleting in-use files is allowed on Unix
{
// [] Deleting a file in use should cause IOException
//-----------------------------------------------------------------
strLoc = "Loc_29yc7";
fs2 = new FileStream(filName, FileMode.Create);
fil2 = new FileInfo(filName);
iCountTestcases++;
try
{
fil2.Delete();
#if !TEST_WINRT // WINRT always sets FILE_SHARE_DELETE
iCountErrors++;
printerr("Error_1y678! Expected exception not thrown");
}
catch (IOException)
{
#endif
}
catch (UnauthorizedAccessException iexc)
{
iCountErrors++;
printerr("Error_1213! This excepton shouldn't occur on Win9X platforms" + iexc.Message);
}
catch (Exception exc)
{
iCountErrors++;
printerr("Error_16709! Incorrect exception thrown, exc==" + exc.ToString());
}
fs2.Dispose();
#if !TEST_WINRT
iCountTestcases++;
if (!fil2.Exists)
{
iCountErrors++;
printerr("Error_768bc! File does not exist==" + fil2.FullName);
}
fil2.Delete();
#endif
fil2.Refresh();
iCountTestcases++;
if (fil2.Exists)
{
iCountErrors++;
printerr("Error_810x8! File not deleted==" + fil2.FullName);
}
//-----------------------------------------------------------------
}
// [] Deleting a file should remove it
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// ][ Deleting a filename with wildcard should work
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
}
//// Finish Diagnostics
//.........这里部分代码省略.........
示例15: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
String strLoc = "Loc_0001";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
String filName = s_strTFAbbrev+"TestFile";
FileInfo fil2;
Stream stream;
if(File.Exists(filName))
File.Delete(filName);
iCountTestcases++;
try {
DateTime dt = File.GetLastAccessTime(null);
iCountErrors++;
printerr( "Error_0002! Expected exception not thrown");
} catch (ArgumentNullException exc){
printinfo("Expected exception thrown" + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0003! Unexpected exceptiont thrown: "+exc.ToString());
}
iCountTestcases++;
try {
DateTime dt = File.GetLastAccessTime("");
iCountErrors++;
printerr( "Error_0004! Expected exception not thrown");
} catch (ArgumentException exc){
printinfo("Expected exception thrown" + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0005! Unexpected exceptiont thrown: "+exc.ToString());
}
strLoc = "Loc_0006";
FileStream fs = new FileStream(filName, FileMode.Create);
fs.Close();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[]{10}, 0, 1);
stream.Close();
Thread.Sleep(4000);
fil2.Refresh();
iCountTestcases++;
try {
DateTime d1 = fil2.LastAccessTime ;
DateTime d2 = DateTime.Today ;
if( d1.Year != d2.Year || d1.Month != d2.Month || d1.Day != d2.Day) {
iCountErrors++;
printerr( "Error_0007! Creation time cannot be correct.. Expected:::" + DateTime.Today.ToString() + ", Actual:::" + fil2.LastAccessTime.ToString());
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0008! Unexpected exceptiont thrown: "+exc.ToString());
}
strLoc = "Loc_0009";
iCountTestcases++;
try {
if( (DateTime.Today - File.GetLastAccessTime(s_strTFName) ).Seconds > 60 ) {
iCountErrors++;
printerr( "Eror_0010! LastAccess time is not correct");
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0011! Unexpected exception thrown: "+exc.ToString());
}
fil2.Delete();
if(File.Exists(filName))
File.Delete(filName);
} catch (Exception exc_general ) {
++iCountErrors;
Console.WriteLine (s_strTFAbbrev + " : Error Err_0012! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
}
if ( iCountErrors == 0 )
{
Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
return true;
}
else
{
Console.WriteLine("FAiL! "+s_strTFName+" ,iCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
return false;
}
}