Ejemplo n.º 1
0
        /// <summary>
        /// If there is a directory or file at the specified path, returns true.
        /// Otherwise, returns false.
        /// Does not throw IO exceptions, to match Directory.Exists and File.Exists.
        /// Unlike calling each of those in turn it only accesses the disk once, which is faster.
        /// </summary>
        internal static bool FileOrDirectoryExistsNoThrow(string fullPath)
        {
            fullPath = AttemptToShortenPath(fullPath);
            if (NativeMethodsShared.IsWindows)
            {
#if !CLR2COMPATIBILITY
                if (Traits.Instance.CacheFileExistence)
                {
                    // Possible future improvement: make sure file existence caching happens only at evaluation time, and maybe only within a build session. https://github.com/Microsoft/msbuild/issues/2306
                    return(FileExistenceCache.GetOrAdd(fullPath, NativeMethodsShared.FileExists));
                }
                else
                {
#endif
                return(NativeMethodsShared.FileExists(fullPath));

#if !CLR2COMPATIBILITY
            }
#endif
            }
            else
            {
                try
                {
                    return(File.Exists(fullPath) || Directory.Exists(fullPath));
                }
                catch
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns if the directory exists
        /// </summary>
        /// <param name="fullPath">Full path to the file in the filesystem</param>
        /// <returns></returns>
        internal static bool FileExistsNoThrow(string fullPath
#if !CLR2COMPATIBILITY
                                               , IFileSystem fileSystem = null
#endif
                                               )
        {
            fullPath = AttemptToShortenPath(fullPath);

            try
            {
#if CLR2COMPATIBILITY
                return(NativeMethodsShared.FileExists(fullPath));
#else
                fileSystem = fileSystem ?? DefaultFileSystem;

                return(Traits.Instance.CacheFileExistence
                    ? FileExistenceCache.GetOrAdd(fullPath, fileSystem.FileExists)
                    : fileSystem.FileExists(fullPath));
#endif
            }
            catch
            {
                return(false);
            }
        }