G
Guest
Greetings all,
Have a question regarding File.Move / FileStreams
My steps,
1. Create file1 demo.log [FileMode.CreateNew]
2. Move file to demo1.log
3. Create file2 with same name as in #1: demo.log [FileMode.CreateNew]
Now, problem is that the filehandle in #1 and #3 are the same.
Are they expected to be?
I suspect this is the reason why file2 inherits the CreateDate from file1
which is my main problem :-/
Can anyone please enlighten me what is going on?
Cheers
Sample code
==================================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileMoveIssue
{
class Program
{
static void Main(string[] args)
{
RecycleLogFile();
}
/// <summary> Illustreates how FileStream.Create after File.Move
gets old file handle</summary>
public static void RecycleLogFile()
{
string path = Path.GetTempPath();
string fileName = "demo";
string extension = ".log";
FileStream fs0 = null;
if (File.Exists(path + fileName + extension))
File.Delete(path + fileName + extension);
if (File.Exists(path + fileName + "1" + extension))
File.Delete(path + fileName + "1" + extension);
fs0 = new FileStream(path + fileName + extension,
FileMode.CreateNew, FileAccess.Write, FileShare.Read);
DateTime file1CreationDate = DateTime.Now.AddDays(-2);
int fileHandle1 = fs0.Handle.ToInt32();
fs0.Close();
File.SetCreationTime(fs0.Name, file1CreationDate); //creation
time should be unique per file
//move file demo.log -> demo1.log
File.Move(fs0.Name, path + fileName + "1" + extension);
if (File.Exists(fs0.Name))
{
Console.WriteLine("File should Not exist it has been moved");
return;
}
FileStream fs1 = new FileStream(path + fileName + extension,
FileMode.CreateNew, FileAccess.Write, FileShare.Read);
int fileHandle2 = fs1.Handle.ToInt32();
Console.WriteLine("filehandle1==filehandle2 {0} filehandle1 {1}
filehandle2 {2}", fileHandle1 == fileHandle2, fileHandle1, fileHandle2);
DateTime file2CreationDate = File.GetCreationTime(fs1.Name);
Console.WriteLine("file1.CreationDate== file1.CreationDate {0}
", file1CreationDate == file2CreationDate);
if (fs0 != null)
fs0.Close();
if (fs1 != null)
fs1.Close();
Console.ReadLine();
}
}
}
Have a question regarding File.Move / FileStreams
My steps,
1. Create file1 demo.log [FileMode.CreateNew]
2. Move file to demo1.log
3. Create file2 with same name as in #1: demo.log [FileMode.CreateNew]
Now, problem is that the filehandle in #1 and #3 are the same.
Are they expected to be?
I suspect this is the reason why file2 inherits the CreateDate from file1
which is my main problem :-/
Can anyone please enlighten me what is going on?
Cheers
Sample code
==================================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileMoveIssue
{
class Program
{
static void Main(string[] args)
{
RecycleLogFile();
}
/// <summary> Illustreates how FileStream.Create after File.Move
gets old file handle</summary>
public static void RecycleLogFile()
{
string path = Path.GetTempPath();
string fileName = "demo";
string extension = ".log";
FileStream fs0 = null;
if (File.Exists(path + fileName + extension))
File.Delete(path + fileName + extension);
if (File.Exists(path + fileName + "1" + extension))
File.Delete(path + fileName + "1" + extension);
fs0 = new FileStream(path + fileName + extension,
FileMode.CreateNew, FileAccess.Write, FileShare.Read);
DateTime file1CreationDate = DateTime.Now.AddDays(-2);
int fileHandle1 = fs0.Handle.ToInt32();
fs0.Close();
File.SetCreationTime(fs0.Name, file1CreationDate); //creation
time should be unique per file
//move file demo.log -> demo1.log
File.Move(fs0.Name, path + fileName + "1" + extension);
if (File.Exists(fs0.Name))
{
Console.WriteLine("File should Not exist it has been moved");
return;
}
FileStream fs1 = new FileStream(path + fileName + extension,
FileMode.CreateNew, FileAccess.Write, FileShare.Read);
int fileHandle2 = fs1.Handle.ToInt32();
Console.WriteLine("filehandle1==filehandle2 {0} filehandle1 {1}
filehandle2 {2}", fileHandle1 == fileHandle2, fileHandle1, fileHandle2);
DateTime file2CreationDate = File.GetCreationTime(fs1.Name);
Console.WriteLine("file1.CreationDate== file1.CreationDate {0}
", file1CreationDate == file2CreationDate);
if (fs0 != null)
fs0.Close();
if (fs1 != null)
fs1.Close();
Console.ReadLine();
}
}
}