Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中创建不带只读属性的目录_C#_Attributes_Directory - Fatal编程技术网

在C#中创建不带只读属性的目录

在C#中创建不带只读属性的目录,c#,attributes,directory,C#,Attributes,Directory,当我尝试这样做时: 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

当我尝试这样做时:

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:
文件资源管理器显示:

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

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

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

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

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

现在我手动创建文件夹。默认情况下,它是只读的

属性-r C:\cmd中的新/s不工作


如果我手动取消选中只读并单击确定,那么如果我单击属性-只读返回,我将验证运行此代码的应用程序本身在Windows中是否具有正确的权限。例如,右键单击应用程序并单击“以管理员身份运行”

尝试使用以下代码:

 Directory.CreateDirectory(@"c:\newfolder");
 File.CreateText(@"c:\newfolder\textfile.txt");
这可能是错误的。当文件路径实际上是文件夹路径时,任何创建文件(或打开流)的尝试都将以结束

UnauthorizedAccessException

这个例外是非常误导人的,让你去看所有错误的地方。已经去过了,看到了。

也许这个只读属性是从某个父文件夹继承的?它直接在根驱动器上。我不知道如何将“C:\”设置为只读。Web/windows应用程序?也许这不是“只读问题”,而是“权限不足问题”?为什么要使用[folderPath]作为File.CreateText的参数?它应该是目录路径+文件名。[folderPath]是模板。这意味着[folderPath]=“任意路径”。我的Visual Studio以管理员权限运行。但如果我以管理员身份运行ConsoleApplication.exe,则会引发异常。如果在两行代码中都使用[folderPath],请确保第二行:“File.CreateText”指向类似“File.txt”的文件
UnauthorizedAccessException