
[英]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.