Setting Windows file security

后端 未结 2 1688
野性不改
野性不改 2020-12-19 12:42

My problem is the opposite that most people have. I\'m generating files locally in C#, but I want them to be marked as blocked. So when a user opens them in an app

2条回答
  •  猫巷女王i
    2020-12-19 13:36

    You can also use the PersistZoneIdentifier object instead of writing the alternative data stream directly.

    More information here: http://blogs.msdn.com/b/oldnewthing/archive/2013/11/04/10463035.aspx and here: https://github.com/citizenmatt/UnblockZoneIdentifier

    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    
    namespace ConsoleApplication3
    {
        public enum URLZONE : uint
        {
            URLZONE_LOCAL_MACHINE = 0,
            URLZONE_INTRANET = 1,
            URLZONE_TRUSTED = 2,
            URLZONE_INTERNET = 3,
            URLZONE_UNTRUSTED = 4,
        }
    
        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("cd45f185-1b21-48e2-967b-ead743a8914e")]
        public interface IZoneIdentifier
        {
            URLZONE GetId();
            void SetId(URLZONE zone);
            void Remove();
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                object persistZoneId = Activator.CreateInstance(Type.GetTypeFromCLSID(Guid.Parse("0968e258-16c7-4dba-aa86-462dd61e31a3")));
                IZoneIdentifier zoneIdentifier = (IZoneIdentifier)persistZoneId;
                IPersistFile persisteFile = (IPersistFile)persistZoneId;
                zoneIdentifier.SetId(URLZONE.URLZONE_UNTRUSTED);
                persisteFile.Save(@"c:\temp\test.txt", false);
            }
        }
    }
    

提交回复
热议问题