@@ -114,22 +114,23 @@ public static string GetAttString(string path)
114114 }
115115
116116 /// <summary>
117- /// Returns true if the file hase a format ~XXXXX.tmp, false - otherwise.
117+ /// Returns true if the file is in ~XXXXX.tmp, pptXXXX.tmp format , false - otherwise.
118118 /// </summary>
119- /// <param name="path">Path to a file or folder .</param>
119+ /// <param name="path">Path to a file.</param>
120120 private static bool IsMsOfficeTemp ( string path )
121121 {
122- return ( Path . GetFileName ( path ) . StartsWith ( '~' ) && Path . GetExtension ( path ) . Equals ( ".tmp" , StringComparison . InvariantCultureIgnoreCase ) ) // Word temp files
123- || ( Path . GetFileName ( path ) . StartsWith ( "ppt" ) && Path . GetExtension ( path ) . Equals ( ".tmp" , StringComparison . InvariantCultureIgnoreCase ) ) // PowerPoint temp files
124- || ( string . IsNullOrEmpty ( Path . GetExtension ( path ) ) && ( Path . GetFileName ( path ) . Length == 8 ) ) ; // Excel temp files
122+ return ( Path . GetFileName ( path ) . StartsWith ( '~' ) && Path . GetExtension ( path ) . Equals ( ".tmp" , StringComparison . InvariantCultureIgnoreCase ) ) // Word temp files
123+ || ( Path . GetFileName ( path ) . StartsWith ( "ppt" ) && Path . GetExtension ( path ) . Equals ( ".tmp" , StringComparison . InvariantCultureIgnoreCase ) ) // PowerPoint temp files
124+ || ( string . IsNullOrEmpty ( Path . GetExtension ( path ) ) && ( Path . GetFileName ( path ) . Length == 8 ) ) // Excel temp files
125+ || ( ( Path . GetFileNameWithoutExtension ( path ) . Length == 8 ) && Path . GetExtension ( path ) . Equals ( ".tmp" , StringComparison . InvariantCultureIgnoreCase ) ) ; // Excel temp files
125126 }
126127
127128 /// <summary>
128129 /// Returns true if file system contains MS Office lock file (~$file.ext) in file
129130 /// system that corresponds to the provided path to MS Office file.
130131 /// </summary>
131132 /// <param name="path">Path to MS Office file.</param>
132- private static bool IsMsOfficeLocked ( string path )
133+ internal static bool IsMsOfficeLocked ( string path )
133134 {
134135 string lockPath = GetLockPathFromMsOfficePath ( path ) ;
135136 return lockPath != null ;
@@ -140,18 +141,18 @@ private static bool IsMsOfficeLocked(string path)
140141 /// Returns true if the provided path points to MS Office lock file (~$file.ext).
141142 /// </summary>
142143 /// <param name="path">Path to lock file.</param>
143- private static bool IsMsOfficeLockFile ( string path )
144+ internal static bool IsMsOfficeLockFile ( string path )
144145 {
145146 return Path . GetFileName ( path ) . StartsWith ( "~$" ) ;
146147 }
147148
148- /*
149- public static string GetMsOfficePathFromLock(string msOfficeLockFilePath)
150- {
151- int separatorIndex = msOfficeLockFilePath.LastIndexOf(Path.DirectorySeparatorChar);
152- return msOfficeLockFilePath.Remove(separatorIndex + 1, "~$".Length);
153- }
154- */
149+
150+ // public static string GetMsOfficePathFromLock(string msOfficeLockFilePath)
151+ // {
152+ // int separatorIndex = msOfficeLockFilePath.LastIndexOf(Path.DirectorySeparatorChar);
153+ // return msOfficeLockFilePath.Remove(separatorIndex + 1, "~$".Length);
154+ // }
155+
155156
156157 /// <summary>
157158 /// Returns MS Office lock file path if such file exists.
@@ -222,6 +223,15 @@ public static bool AvoidSync(string path)
222223 return IsMsOfficeLockFile ( path ) || IsMsOfficeLocked ( path ) || IsMsOfficeTemp ( path ) || IsHiddenOrTemp ( path ) ;
223224 }
224225
226+ /// <summary>
227+ /// Returns true if the file or folder should not be automatically locked. False - otherwise.
228+ /// </summary>
229+ /// <param name="path">Path to a file or folder.</param>
230+ public static bool AvoidAutoLock ( string path )
231+ {
232+ return IsMsOfficeLockFile ( path ) || IsMsOfficeTemp ( path ) || IsHiddenOrTemp ( path ) ;
233+ }
234+
225235 /// <summary>
226236 /// Gets formatted file size or null for folders or if the file is not found.
227237 /// </summary>
@@ -253,5 +263,27 @@ public static string Size(string path)
253263 double num = Math . Round ( bytes / Math . Pow ( 1024 , place ) , 1 ) ;
254264 return ( Math . Sign ( length ) * num ) . ToString ( ) + suf [ place ] ;
255265 }
266+
267+ /// <summary>
268+ /// Returns true if the file is locked for writing, false otherwise.
269+ /// </summary>
270+ /// <param name="path">Path ot a file.</param>
271+ /// <returns>True if the file is locked for writing. False otherwise.</returns>
272+ public static bool IsWriteLocked ( string path )
273+ {
274+ try
275+ {
276+ using ( FileStream stream = File . Open ( path , FileMode . Open , FileAccess . Read , FileShare . Read | FileShare . Delete ) )
277+ {
278+ stream . Close ( ) ;
279+ }
280+ }
281+ catch ( IOException )
282+ {
283+ return true ;
284+ }
285+
286+ return false ;
287+ }
256288 }
257289}
0 commit comments