public static Boolean IsFileLocked(VirtualFilePath vfp, FileAccess accessNeeded) { FileStream stream = null; FileInfo file = new FileInfo(vfp.Path); Boolean ret = false; try { stream = file.Open(FileMode.Open, accessNeeded, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) ret = true; } finally { if (stream != null) { stream.Close(); } } return(ret); }
public static void CopyFile ( VirtualFilePath sourceFile, string targetFilePath, CollisionRemediation cr) { CopyFileWorker(sourceFile.Path, targetFilePath, cr); }
/// <summary> /// Converts a string to a VirtualFilepath instance /// </summary> /// <param name="filePath"> /// Can be a local path (in which case the local machine is the host) or "MACHINENAME,c:\local\path" /// </param> /// <param name="throwOnInvalidPath"> /// throw exception if the path is not syntactically valid /// </param> /// <param name="throwIfNotDirectoryPath">throw exception is path is to a file</param> /// <param name="throwOnNotExists">throw exception if the directory or file does not exist</param> /// <returns>A VirtualFilePath object</returns> public static VirtualFilePath Parse ( string filePath, Boolean throwOnInvalidPath = false, Boolean throwOnNotExists = false) { string hostName = System.Environment.MachineName; string localPath = null; #region Validate if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath)); } else if (filePath.StartsWith(@"\\")) { throw new fr.ScarabException("UNC paths are not supported"); } if (filePath.Contains(",")) { var split = filePath.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); hostName = split[0]; localPath = System.IO.Path.GetFullPath(split[1]); } else { localPath = System.IO.Path.GetFullPath(filePath); } if (!CommonRegex.LocalFsoPathRegex.IsMatch(localPath)) { if (throwOnInvalidPath) { throw new ScarabException("Failed to parse path '{0}'", filePath); } else { return(null); } } #endregion VirtualFilePath ret = new VirtualFilePath ( hostName, localPath, throwIfNotFilePath: throwOnInvalidPath, throwOnNotExists: throwOnNotExists ); Debug.Assert(ret != null); return(ret); }
public override object ConvertFrom ( ITypeDescriptorContext ctx, CultureInfo ci, object data) { VirtualFilePath vfp = VirtualFilePath.Parse ( data.ToString(), true, // Just 'throwOnInvalidPath' can be true, since file paths or false ); Debug.Assert(vfp != null); return(vfp); }
public override Boolean Equals(object obj) { // If parameter is null return false if (obj == null) { return(false); } VirtualFilePath other = obj as VirtualFilePath; if ((System.Object)other == null) { return(false); } // Return true if the fields match (may be referenced by derived classes) return(this.GetHashCode() == other.GetHashCode()); }
/// <summary> /// This code performs the actual conversion from a VirtualFilePath to an InstanceDescriptor. /// </summary> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(InstanceDescriptor)) { ConstructorInfo ci = typeof(VirtualFilePath).GetConstructor ( new Type[] { typeof(string), typeof(string), typeof(Boolean) } ); VirtualFilePath t = ( VirtualFilePath )value; return(new InstanceDescriptor(ci, new object[] { t.HostName, t.LocalPath, false })); } // Always call base, even if you can't convert. return(base.ConvertTo(context, culture, value, destinationType)); }