Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am am learning CLR And would like to delete file from SQL SP

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.
Posted
Updated 23-Mar-16 5:21am
v2
Comments
F-ES Sitecore 23-Mar-16 8:27am    
You might have access but the account your code is running under probably doesn't.
ZurdoDev 23-Mar-16 10:26am    
F-ES is right. The account it's running as doesn't have permissions.
Philippe Mori 23-Mar-16 21:22pm    
You should not delete files from SQL. Let SP manage data only... Doing "unrelated" things make the code less maintainable or reusable.

However, if you want to do it, it is up to you to ensure that file are deletable from the account used by SQL.

1 solution

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)[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900