C# File.SetAttributes的代码示例

File.SetAttributes方法的主要功能描述

设置指定路径上文件的指定 FileAttributes。

通过代码示例来学习C# File.SetAttributes方法

通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。


File.SetAttributes是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的File.SetAttributes() 已经帮大家高亮显示了,大家可以重点学习File.SetAttributes() 方法的写法,从而快速掌握该方法的应用。

File.SetAttributes的代码示例1 - OpenForWrite()

    using System.IO;

        private static SafeFileHandle OpenForWrite(ITracer tracer, string fileName)
        {
            SafeFileHandle handle = CreateFile(fileName, FileAccess.Write, FileShare.None, IntPtr.Zero, FileMode.Create, FileAttributes.Normal, IntPtr.Zero);
            if (handle.IsInvalid)
            {
                // If we get a access denied, try reverting the acls to defaults inherited by parent
                if (Marshal.GetLastWin32Error() == AccessDeniedWin32Error)
                {
                    tracer.RelatedEvent(
                        EventLevel.Warning,
                        "FailedOpenForWrite",
                        new EventMetadata
                        {
                            { TracingConstants.MessageKey.WarningMessage, "Received access denied. Attempting to delete." },
                            { "FileName", fileName }
                        });

                    File.SetAttributes(fileName, FileAttributes.Normal);
                    File.Delete(fileName);

                    handle = CreateFile(fileName, FileAccess.Write, FileShare.None, IntPtr.Zero, FileMode.Create, FileAttributes.Normal, IntPtr.Zero);
                }
            }

            return handle;
        }
    

开发者ID: microsoft,   项目名称: VFSForGit,   代码行数: 28,   代码来源: NativeMethods.cs

在microsoft提供的OpenForWrite()方法中,该源代码示例一共有28行, 其中使用了File.SetAttributes()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes的代码示例2 - PrefetchBuildsIdxWhenMissingFromPrefetchPack()

    using System.IO;

        [TestCase, Order(2)]
        public void PrefetchBuildsIdxWhenMissingFromPrefetchPack()
        {
            string[] prefetchPacks = this.ReadPrefetchPackFileNames();
            prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack");

            string idxPath = Path.ChangeExtension(prefetchPacks[0], ".idx");
            idxPath.ShouldBeAFile(this.fileSystem);
            File.SetAttributes(idxPath, FileAttributes.Normal);
            this.fileSystem.DeleteFile(idxPath);
            idxPath.ShouldNotExistOnDisk(this.fileSystem);

            // Prefetch should rebuild the missing idx
            this.Enlistment.Prefetch("--commits");
            this.PostFetchJobShouldComplete();

            idxPath.ShouldBeAFile(this.fileSystem);

            // All of the original prefetch packs should still be present
            string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
            newPrefetchPacks.ShouldContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
            this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
            this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
        }
    

开发者ID: microsoft,   项目名称: VFSForGit,   代码行数: 26,   代码来源: PrefetchVerbWithoutSharedCacheTests.cs

在microsoft提供的PrefetchBuildsIdxWhenMissingFromPrefetchPack()方法中,该源代码示例一共有26行, 其中使用了File.SetAttributes()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes的代码示例3 - CanSaveAsCopyReadOnlyFile()

    using System.IO;

        [Test]
        public void CanSaveAsCopyReadOnlyFile()
        {
            using (var original = new TemporaryFile())
            {
                try
                {
                    using (var copy = new TemporaryFile())
                    {
                        // Arrange
                        using (var wb = new XLWorkbook())
                        {
                            var sheet = wb.Worksheets.Add("TestSheet");
                            wb.SaveAs(original.Path);
                        }
                        File.SetAttributes(original.Path, FileAttributes.ReadOnly);

                        // Act
                        using (var wb = new XLWorkbook(original.Path))
                        {
                            wb.SaveAs(copy.Path);
                        }

                        // Assert
                        Assert.IsTrue(File.Exists(copy.Path));
                        Assert.IsFalse(File.GetAttributes(copy.Path).HasFlag(FileAttributes.ReadOnly));
                    }
                }
                finally
                {
                    // Tear down
                    File.SetAttributes(original.Path, FileAttributes.Normal);
                }
            }
        }
    

开发者ID: ClosedXML,   项目名称: ClosedXML,   代码行数: 37,   代码来源: SavingTests.cs

在ClosedXML提供的CanSaveAsCopyReadOnlyFile()方法中,该源代码示例一共有37行, 其中使用了File.SetAttributes()2次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes的代码示例4 - SetHost()

    using System.IO;

        /// 
        /// 
        /// 
        /// 
        /// 空值表示删除对应的host记录
        /// 测试用
        public static void SetHost(string host, string ip, string hostsPath = null) {
            GetIp(host, out long position, hostsPath);
            if (position == -2) {
                File.WriteAllText(hostsPath, $"{ip} {host}");
                return;
            }
            if (string.IsNullOrEmpty(hostsPath)) {
                hostsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers\\etc\\hosts");
            }
            //通常情况下这个文件是只读的,所以写入之前要取消只读
            File.SetAttributes(hostsPath, File.GetAttributes(hostsPath) & (~FileAttributes.ReadOnly));
            byte[] buffer = new byte[]{ };
            using (MemoryStream ms = new MemoryStream())
            using (StreamWriter sw = new StreamWriter(ms))
            using (FileStream fs = new FileStream(hostsPath, FileMode.OpenOrCreate, FileAccess.Read))
            using (StreamReader sr = new StreamReader(fs)) {
                bool writed = false;
                while (true) {
                    long p = sr.BaseStream.Position;
                    string line = sr.ReadLine();
                    if (p == position) {
                        if (!string.IsNullOrEmpty(ip)) {
                            sw.WriteLine($"{ip} {host}");
                        }
                        writed = true;
                    }
                    else {
                        sw.WriteLine(line);
                    }
                    if (sr.EndOfStream) {
                        break;
                    }
                }
                if (!writed) {
                    sw.WriteLine($"{ip} {host}");
                }
                sw.Flush();
                buffer = ms.ToArray();
            }
            File.WriteAllBytes(hostsPath, buffer);
        }
    

开发者ID: ntminer,   项目名称: NtMiner,   代码行数: 49,   代码来源: Hosts.cs

在ntminer提供的SetHost()方法中,该源代码示例一共有49行, 其中使用了File.SetAttributes()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes的代码示例5 - CreateSymbolicLink()

    using System.IO;

        public static bool CreateSymbolicLink(string source, string target, bool isDirectory)
        {
            bool success = false;
            try
            {
                // In case the file is marked as readonly
                if (File.Exists(source))
                {
                    File.SetAttributes(source, FileAttributes.Normal);
                    File.Delete(source);
                }
                else if (Directory.Exists(source))
                {
                    Directory.Delete(source);
                }

                int releaseId = int.Parse(GetRegistryLocalMachineSubKeyValue(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "0"));

                int flags = isDirectory ? SYMBOLIC_LINK_FLAG_DIRECTORY : SYMBOLIC_LINK_FLAG_FILE;
                if (releaseId >= 1703) // Verify that the Windows build is equal or above 1703, as SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE was introduced at that version. Using it on older version will cause an error 87 and symlinks won't be created
                    flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;

                success = CreateSymbolicLink(source, target, flags);
            }
            catch { }
            return success;
        }
    

开发者ID: ubisoft,   项目名称: Sharpmake,   代码行数: 29,   代码来源: Util.cs

在ubisoft提供的CreateSymbolicLink()方法中,该源代码示例一共有29行, 其中使用了File.SetAttributes()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes的代码示例6 - FilePreparePath()

    using System.IO;
#endif
#if NET45 || NETSTANDARD1_3 || NETSTANDARD1_6
        /// 
        /// Loads an HTML document from an Internet resource and saves it to the specified XmlTextWriter.
        /// 
        /// The requested URL, such as "http://Myserver/Mypath/Myfile.asp".
        /// The XmlTextWriter to which you want to save to.
        public void LoadHtmlAsXml(string htmlUrl, XmlWriter writer)
        {
            HtmlDocument doc = Load(htmlUrl);
            doc.Save(writer);
        }
#endif

        #endregion

        #region Private Methods

        private static void FilePreparePath(string target)
        {
            if (File.Exists(target))
            {
                FileAttributes atts = File.GetAttributes(target);
                File.SetAttributes(target, atts & ~FileAttributes.ReadOnly);
            }
            else
            {
                string dir = Path.GetDirectoryName(target);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }
        }
    

开发者ID: zzzprojects,   项目名称: html-agility-pack,   代码行数: 35,   代码来源: HtmlWeb.cs

在zzzprojects提供的FilePreparePath()方法中,该源代码示例一共有35行, 其中使用了File.SetAttributes()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.SetAttributes()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.SetAttributes()可能更有帮助。

File.SetAttributes()方法的常见问题及解答

如何使用C#设置指定路径上文件的指定 FileAttributes。

在C#中,一般可以使用System.IO中的File.SetAttributes方法来实现; 本文中提供了7个File.SetAttributes的示例, 你看一下是否对你有帮助。

C#中File.SetAttributes的用法说明

File.SetAttributes主要用于设置指定路径上文件的指定 FileAttributes。, 它是System.IO中的一个常见方法,您也可以参考官方帮助文档获取更详细的用法说明。

C#中File.SetAttributes()的常见错误类型及注意事项

File.SetAttributes的错误类型有很多, 这里就不一一阐述了,本文只列出一些常见的代码示例供参考,大家可以看一下代码中Catch语句中是否有常见的错误捕获及处理。

C#中File.SetAttributes()的构造函数有哪些

File.SetAttributes构造函数功能基本类似,只是参数不同; 目前主流的集成开发环境都已经带智能提醒了,如:Visual Studio; 大家可以非常轻松的通过Visual Studio中的智能提醒,了解对应构造函数的用法。

如何使用ChartGPT写一段File.SetAttributes的代码

你可以在ChartGPT中输入如下的指令:"提供一个如何使用File.SetAttributes的C#代码示例"
ChartGPT写出的代码和本文中的小编提供的代码的区别。 ChartGPT发展到现在已经非常聪明了,但需要使用这提供非常专业的问题,才可能有比较好的源代码示例; 而本文中, 小编已经帮您列出来基本所有类和所有方法的使用示例, 而且这些示例基本都是一些网络大佬提供的源码,可以更方便的供一些开发菜鸟或者资深开发参考和学习。

File.SetAttributes所在的类及名称空间

File.SetAttributes是System.IO下的方法。

什么是File.SetAttributes?

File.SetAttributes是System.IO下的一个方法, 一般用于设置指定路径上文件的指定 FileAttributes。

File.SetAttributes怎么使用?

File.SetAttributes使用上比较简单,可以参考MSDN中的帮助文档,也参考本文中提供的7个使用示例。

File.SetAttributes菜鸟教程

对于菜鸟来说,本文中提供的7个File.SetAttributes写法都将非常直观的帮您掌握File.SetAttributes的用法,是一个不错的参考教程。

本文中的File.SetAttributes方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。