2011-04-01 85 views
0

當調用CreateFile()函數時,它返回錯誤代碼0x5,這意味着訪問被拒絕。任何人都可以解決這個問題嗎?調用CreateFile()時訪問被拒絕()

注意:CreateFile()讀取快照的路徑,文件路徑爲\?\ globalroot \ device \ harddiskvolumeshadowcopy35 \ program files \ common files \ microsoft shared \ web server extensions extensions \ 12 \ admisapi。

非常感謝。

+0

什麼是你新文件的位置?你是以管理員還是以用戶身份運行應用程序?在Windows Vista或最近您需要提升權限才能在位置創建文件,如Program Files – 2011-04-01 09:20:09

+0

它被稱爲UAC;看到[我的答案在這裏](http://stackoverflow.com/questions/5210575/does-windows-7-have-the-same-problem-as-vista/5210642#5210642)瞭解更多詳情。您不應該首先寫入系統文件夾。但是,如果你絕對必須,我只是寫了一個關於如何在C#中提升應用程序過程的詳細解釋[回覆此問題](http://stackoverflow.com/questions/5507818/workaround-against-registry-manipulation -limitation式 - 窗口五百五十零萬八千分之七#5508000)。 – 2011-04-01 09:22:52

回答

0
ConnectionOptions connection = new ConnectionOptions(); 

//just username, without domain name, otherwise, a "RPC is Unavaliable." exception will be thrown. 
connection.Username = "testUser"; 

connection.Password = "testPassword"; 

//Allow privilege 
connection.EnablePrivileges = true; 

connection.Impersonation = ImpersonationLevel.Delegate; 

connection.Authentication = AuthenticationLevel.Call; 

//Neither ntdlmdomain or kerberoes, otherwise, a "Invalid Parameter." exception will be thrown. 
connection.Authority = "ntlmdomain:MYDOMAIN"; 

//IP Address or host full name. 
ManagementScope scope = new ManagementScope("\\\\myIPAddress\\root\\CIMV2", connection); 

scope.Connect(); 

ManagementClass classInstance = new ManagementClass(scope,new ManagementPath("Win32_Process"), null); 

ManagementBaseObject inParams = classInstance.GetMethodParameters("Create"); 

//Change it to your own execute file path 
inParams["CommandLine"] = "myExecuteFilePath"; 

ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null); 
0

你可以手動創建該文件嗎?這很可能是一個權限問題。

0

這意味着您沒有足夠權限從此文件讀取。檢查文件權限。

0

訪問被拒絕,您的應用程序在哪裏嘗試創建該文件?如果它在程序文件等中,可能是因爲它的Windows 7和用戶無法首先提升權限而無法創建它。另外,確保它在你認爲的地方創建它。

+0

文件路徑是\\?\ globalroot \ device \ harddiskvolumeshadowcopy33 \ program files \ common files \ microsoft shared \ web server extensions \ 12 \ admisapi – Daniel 2011-04-05 01:25:43

+0

並且如上所述,如果您嘗試使用命令行將文件寫入該文件。它工作嗎?例如回聲「測試」到它的結尾或某事 – BugFinder 2011-04-05 06:58:06