gpt4 book ai didi

c# - 在 C# 中创建没有只读属性的目录

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:21 26 4
gpt4 key购买 nike

当我尝试这样做时:

Directory.CreateDirectory([folderPath]);
File.CreateText([folderPath]);

我有异常(exception) -

System.UnauthorizedAccessException was unhandled by user code
HResult=-2147024891
Message=Access to the path 'C:\[folderPath]' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at System.IO.File.CreateText(String path)
at Contest.Service.FileSystem.CreateFileAndFolderForAttempt(IAttempt attempt) in c:\Users\Mif\Documents\Visual Studio 2012\Projects\Contest\Contest.Service\FileSystem.cs:line 33
at Contest.Service.Tests.FileSystemTests.TestCreateFileAndFolderForAttempt() in c:\Users\Mif\Documents\Visual Studio 2012\Projects\Contest\Contest.Service.Tests\UnitTest1.cs:line 32
InnerException:

文件资源管理器显示:

属性:[三态/不确定复选框]只读(仅适用于文件夹中的文件)

但是 Debug.WriteLine(CurrentContestDirectory.Attributes.ToString());显示 - 目录。而不是只读属性!

我在 Google 中寻找“使用 C# 删除目录上的只读属性”,但所有答案都没有帮助我。我需要做什么?

所有代码都在装有 Windows 8 的本地计算机上运行。管理员帐户。如果我手动取消选中 readonly attrinute,代码运行后它会切换回来。

这是一个简单的控制台应用程序。

现在我手动创建文件夹。它默认为只读。

attrib -r C:\new/s in cmd 不工作!!!

如果我手动取消选中只读并单击确定,那么如果我单击属性 - 只读返回

最佳答案

试试这段代码:

 Directory.CreateDirectory(@"c:\newfolder");
File.CreateText(@"c:\newfolder\textfile.txt");

关于c# - 在 C# 中创建没有只读属性的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com