CLR C#或VB .NET删除文件 [英] CLR C# or VB .NET to delete file
问题描述
我正在学习CLR并希望从SQL SP中删除文件
文件位于不同的服务器上。
任何帮助都会被挪用。
先谢谢。
< b>我尝试了什么:
i尝试了以下代码并遵守。
Imports System.IO
公共类CLR函数
公共共享功能DeleteFiles(sPath As String)作为整数
File.Delete(sPath)
结束功能
结束班级
从'C:\Temp \CLRFunctions.dll'创建汇编CLRFunctions
WITH PERMISSION_SET = UNSAFE
GO
创建功能dbo.DeleteFiles
(
@FolderPath AS NVA RCHAR(100)
)
RETURNS整数
作为外部名称CLRFunctions.CLRFunctions.DeleteFiles
GO
ALTER DATABASE testBackupdb20150420 SET值得信赖
我收到以下错误。
消息6522消息,16级,状态2,行1
在执行用户定义的例程或聚合DeleteFiles期间发生.NET Framework错误:
System.UnauthorizedAccessException:访问路径'\\\ new-name \ store \ admin \Test \123\text \FileName.Ext'被拒绝。
System.UnauthorizedAccessException:
at System.IO .__ Error.WinIOError(Int32 errorCode ,String maybeFullPath)
at System.IO.File.InternalDelete(String path,Boolean checkHost)
。
我有权访问这个位置。
I am am learning CLR And would like to delete file from SQL SP 这篇关于CLR C#或VB .NET删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
但现在,让我们考虑一下你真正需要删除受文件权限保护的文件的情况系统。但首先,您需要了解UAC:用户帐户控制 - 维基百科,免费的百科全书 [ ^ ]。
什么都不能打破访问保护,直到用户明确给出系统UAC确认来提升权限,即使此人已经以管理员身份登录;例如:
http://4sysops.com/archives/vista%E2%80%99s-uac-8-ways-how-to-elevate-an-application-to -run-it-with-administrator-rights [ ^ ],
以管理员身份运行 - Windows 7帮助论坛 [ ^ ]。
您还可以做的唯一事情是从一开始就请求您的应用程序提升权限。为此,您必须创建并嵌入适当的应用程序清单。这在此解释:步骤6:创建和嵌入应用程序清单(UAC) [ ^ ]。
File located are on different server.
Any help would be much appropriated.
Thanks in Advance.
What I have tried:
i have tried following code and complied.
Imports System.IO
Public Class CLRFunctions
Public Shared Function DeleteFiles(sPath As String) As Integer
File.Delete( sPath )
End Function
End Class
CREATE ASSEMBLY CLRFunctions FROM 'C:\Temp\CLRFunctions.dll'
WITH PERMISSION_SET = UNSAFE
GO
CREATE FUNCTION dbo.DeleteFiles
(
@FolderPath AS NVARCHAR(100)
)
RETURNS integer
AS EXTERNAL NAME CLRFunctions.CLRFunctions.DeleteFiles
GO
ALTER DATABASE testBackupdb20150420 SET trustworthy ON
and i am getting following error.
Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "DeleteFiles":
System.UnauthorizedAccessException: Access to the path '\\server-Name\store\admin\Test\123\text\FileName.Ext' is denied.
System.UnauthorizedAccessException:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
.
I do have access to this location.Most likely, you simply should not delete the file the user don't have permission to delete. This would be the best solution in most cases. Know legitimate directories and files, and those which you should not touch.
But now, let's consider the cases when you really need to delete the file protected by the file permissions system. But first, you need to understand UAC: User Account Control — Wikipedia, the free encyclopedia[^].
Nothing can break the access protection until the user explicitly gives the system UAC confirmation to elevate the privilege, even if this person already logged on as an administrator; see, for example:
http://4sysops.com/archives/vista%E2%80%99s-uac-8-ways-how-to-elevate-an-application-to-run-it-with-administrator-rights[^],
Run as Administrator — Windows 7 Help Forums[^].
The only thing you can additionally do is requesting privilege elevation by your application from the very beginning. To do so, you have to create and embed appropriate application manifest. This is explained here: Step 6: Create and Embed an Application Manifest (UAC)[^].