[英]Create directory in C# without readonly attribute
When I try to do that: 当我尝试这样做时:
Directory.CreateDirectory([folderPath]);
File.CreateText([folderPath]);
I had the 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:
File Explorer show me: 文件资源管理器向我展示:
Attributes: [tristate/indeterminate checkbox] Read-only (Only applies to files in folder) 属性:[tristate / indeterminate复选框]只读(仅适用于文件夹中的文件)
But Debug.WriteLine(CurrentContestDirectory.Attributes.ToString());
但是
Debug.WriteLine(CurrentContestDirectory.Attributes.ToString());
Show me - Directory
. 显示给我-
Directory
。 And not the readonly attribute! 而不是readonly属性!
I looked for "Removing read only attribute on a directory using C#" in Google, but all answers don't help me. 我在Google中寻找了“使用C#删除目录上的只读属性”,但是所有答案都对我没有帮助。 What I need to do?
我需要做什么?
All code runs on my local machine with Windows 8. Admin account. 所有代码都在使用Windows 8的本地计算机上运行。Admin帐户。 And if I uncheck readonly attrinute manually, after code run it toggle back.
如果我手动取消选中只读属性,则在代码运行后将其切换回去。
It's simple console app. 这是简单的控制台应用程序。
Now I manually create folder. 现在,我手动创建文件夹。 It's have read-only by default.
默认情况下它是只读的。
attrib -r C:\\new /s in cmd not working!!! attrib -r C:\\ cmd中的new / s不起作用!!!
And if I uncheck read-only manually and click OK then if I click Properties - read-only come back 如果我手动取消选中只读,然后单击确定,那么如果我单击属性- 只读,请回来
try with this code: 尝试使用以下代码:
Directory.CreateDirectory(@"c:\newfolder");
File.CreateText(@"c:\newfolder\textfile.txt");
I would verify that your application running this code itself has proper rights in Windows. 我将验证您运行此代码的应用程序本身在Windows中具有适当的权限。 For instance, right click the application and click "Run as administrator"
例如,右键单击该应用程序,然后单击“以管理员身份运行”
File.CreateText([folderPath]);
is probably wrong. 可能是错误的。 Any attempt to create a file (or open a stream) when the file path is actually a folder path will end with
当文件路径实际上是文件夹路径时,任何创建文件(或打开流)的尝试都将以
UnauthorizedAccessException
The exception is very misleading and ask you to look in all the wrong places. 例外非常容易误导您,请您将其放在所有错误的地方。 Been there, seen it.
去过那里,看到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.