none
C# how to check if a file is blocked RRS feed

  • Question

  • Is there a way in C# that you can check to see if a file is blocked because it was moved from another computer?

    

    Thursday, December 20, 2018 3:20 AM

Answers

  • Yes!

    I think I have some code that will work with using Trinet.Core.IO.Ntfs; from Nuget:

      <package id="Trinet.Core.IO.Ntfs" version="4.1.1" targetFramework="net461" />

            protected void btnProcess_Click(object sender, EventArgs e)
            {
                DirectoryInfo d = new DirectoryInfo(@"C:\MyLocation");//Assuming Test is your Folder
                FileInfo[] Files = d.GetFiles("*.dll"); //get all wanted files
    
                IList<FileInfo> listWithZoneIdentifier = new List<FileInfo>();
                IList<FileInfo> withoutListWithZoneIdentifier = new List<FileInfo>();
                foreach (FileInfo file in Files)
                {
                    if (file.AlternateDataStreamExists("Zone.Identifier"))
                    {
                        AlternateDataStreamInfo s = file.GetAlternateDataStream("Zone.Identifier",
                                                FileMode.Open);
    
                        string fullText = string.Empty;
    
                        using (TextReader reader = s.OpenText())
                        {
                            fullText = string.Format(reader.ReadToEnd());
    
                            if (fullText.Contains("ZoneId=3") == true)
                            {
                                listWithZoneIdentifier.Add(file);
                            }
                            else
                            {
                                withoutListWithZoneIdentifier.Add(file);
                            }
                        }
                    }
                    else
                    {
                        withoutListWithZoneIdentifier.Add(file);
                    }
                }
    
                Debug.WriteLine(string.Format("Total count from listWithZoneIdentifier: {0}", listWithZoneIdentifier.Count));
                Debug.WriteLine(string.Format("Total count from withoutListWithZoneIdentifier: {0}", withoutListWithZoneIdentifier.Count));
    
                if(listWithZoneIdentifier.Count > 0)
                {
                    foreach(FileInfo fileInfo in listWithZoneIdentifier)
                    {
                        fileInfo.DeleteAlternateDataStream("Zone.Identifier");
                    }
                }
    
            }


    • Marked as answer by ttaylor29 Thursday, December 20, 2018 4:00 AM
    • Edited by ttaylor29 Thursday, December 20, 2018 1:34 PM updated comments
    Thursday, December 20, 2018 3:59 AM