Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Data.SqlTypes;
- using Microsoft.SqlServer.Server;
- using System.Security.Principal;
- public partial class StoredProcedures
- {
- [Microsoft.SqlServer.Server.SqlProcedure]
- public static void FileExists(SqlString fileName, out SqlInt32 returnValue)
- {
- WindowsImpersonationContext originalContext = null;
- try
- {
- WindowsIdentity callerIdentity = SqlContext.WindowsIdentity;
- originalContext = callerIdentity.Impersonate();
- if (System.IO.File.Exists(Convert.ToString(fileName)))
- {
- returnValue = 1;
- }
- else
- {
- returnValue = 0;
- }
- }
- catch (Exception)
- {
- returnValue = -1;
- }
- finally
- {
- if (originalContext != null)
- {
- originalContext.Undo();
- }
- }
- }
- }
- USE master
- GO
- CREATE ASYMMETRIC KEY FileUtilitiesKey FROM EXECUTABLE FILE = 'J:FileUtilities.dll'
- CREATE LOGIN CLRLogin FROM ASYMMETRIC KEY FileUtilitiesKey
- GRANT EXTERNAL ACCESS ASSEMBLY TO CLRLogin
- ALTER DATABASE database SET TRUSTWORTHY ON;
- DECLARE @i INT
- --EXEC FileExists '\\server\share\folder\file.dat', @i OUT
- EXEC FileExists 'j:\file.dat', @i OUT
- SELECT @i
Add Comment
Please, Sign In to add comment