I am using a FileSystemWatcher to find any created directories in a directory. Specifically, I use another function to find whether or not the file created is a directory, and if it is a directory, I pass the file path onto another function that checks the size of the directory.
i.e., Directory created > check if it's a directory > if it is, check the size of the directory.
Directory file size function:
long DirectorySize(DirectoryInfo d)
{
long Size = 0;
// Add file sizes.
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
Size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
Size += DirectorySize(di);
}
return (Size);
}
And the simple check for discovering if a file is a directory:
bool isDirectory(string pathName)
{
// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(pathName);
//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
return true;
else
return false;
}
However there's an issue. I want the file to be completely written before I perform the check file size function. Once, and only once, the file is completely written should the check be performed.
I know that code exists such as this, however it's slightly off of the topic of what I want to do, because I don't want to open the file, only find its size. Would there be a way to check that the file has completely written before checking its size?