c# - Why does the Name Property of FileInfo object start with "~$"? -
so i'm writing code going through directory of .xlsx
files , picking file created last. it's simple task, there bit strange happening name
property of particular fileinfo
object , potentially there more cases of occurring.
here code:
directoryinfo di = new directoryinfo(folderpath); fileinfo[] filearray = di.getfiles("*.xlsx", searchoption.alldirectories); if (filearray.count() != 0) { datetime latestdate = datetime.minvalue; string filename = string.empty; foreach (fileinfo file in filearray) { if (file.creationtime > latestdate) { latestdate = file.creationtime; filename = file.fullname; } } }
the filename
important because use query latest file information , display it. however, name
property of particular .xlsx file (potentially more) appearing ~$file.xlsx
when in fact file name file.xlsx
. causes fullname
property contain these characters well.
is there way fix this? triggers this?
opening xlsx file results in excel creating hidden file same name preceded "~$". if 1 of these excel files open @ time retrieve content of directory, temp file.
add filter excludes hidden files , issue fixed.
example:
fileattributes attributes = file.getattributes(path); if((attributes & fileattributes.hidden) == fileattributes.hidden) { // hidden file, skip }
from http://msdn.microsoft.com/en-us/library/system.io.file.getattributes.aspx