
[英]c# get method for a readonly attribute without creating an extra attribute
[英]Create directory in C# without readonly attribute
当我尝试这样做时:
Directory.CreateDirectory([folderPath]);
File.CreateText([folderPath]);
我有一个例外-
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:
文件资源管理器向我展示:
属性:[tristate / indeterminate复选框]只读(仅适用于文件夹中的文件)
但是Debug.WriteLine(CurrentContestDirectory.Attributes.ToString());
显示给我- Directory
。 而不是readonly属性!
我在Google中寻找了“使用C#删除目录上的只读属性”,但是所有答案都对我没有帮助。 我需要做什么?
所有代码都在使用Windows 8的本地计算机上运行。Admin帐户。 如果我手动取消选中只读属性,则在代码运行后将其切换回去。
这是简单的控制台应用程序。
现在,我手动创建文件夹。 默认情况下它是只读的。
attrib -r C:\\ cmd中的new / s不起作用!!!
如果我手动取消选中只读,然后单击确定,那么如果我单击属性- 只读,请回来
尝试使用以下代码:
Directory.CreateDirectory(@"c:\newfolder");
File.CreateText(@"c:\newfolder\textfile.txt");
我将验证您运行此代码的应用程序本身在Windows中具有适当的权限。 例如,右键单击该应用程序,然后单击“以管理员身份运行”
File.CreateText([folderPath]);
可能是错误的。 当文件路径实际上是文件夹路径时,任何创建文件(或打开流)的尝试都将以
UnauthorizedAccessException
例外非常容易误导您,请您将其放在所有错误的地方。 去过那里,看到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.