Пример #1
0
    /// <summary>
    /// Determines whether the file is locked.
    /// </summary>
    /// <param name="file">The file.</param>
    /// <returns>
    ///   <c>true</c> if [is file locked] [the specified file]; otherwise, <c>false</c>.
    /// </returns>
    public static bool CheckFileLocked(this FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException)
        {
            if (!file.CheckExists())
            {
                throw new FileNotFoundException("File {0} not found".Build(file.FullName), file.FullName);
            }
            //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)
            return(true);
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
        }

        //file is not locked
        return(false);
    }
Пример #2
0
        public void ExistsCheckFileInfoTest()
        {
            var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify), "tempfileinfotest.dat");

            _ = RandomData.GenerateFile(fileName, 500);

            var fileInfo = new FileInfo(fileName);

            try
            {
                fileInfo.CheckExists(true);
            }
            catch
            {
                Assert.Fail();
            }

            FileInfo nullFileInfo = null;

            _ = Assert.ThrowsException <NullReferenceException>(() => nullFileInfo.CheckExists(true));

            _ = Assert.ThrowsException <FileNotFoundException>(() => new FileInfo("fakefile").CheckExists(true));
        }
Пример #3
0
 public static bool FileHasInvalidChars(FileInfo file)
 {
     return(file.CheckExists() && file.ArgumentNotNull().FullName.IndexOfAny(InvalidFileNameChars.ToArray()) != -1);
 }