I was able to create the function I needed. For non-SDE workspaces it tests the effective permission of the currentuser to see if they have security policy permissions to write to a directory workspace.The other code for non-SDE workspaces I posted above that tests the read only and can edit properties also should be used, but that code only tests if the read only setting was applied directly to the individual file or directory. That setting would prevent even the owner with full rights over the file or directory from being able to write to it without unchecking the flag.The function below incorporates all of the tests I have mentioned and should work with any IDataset variable, whether it is assigned, unassigned. SDE or non-SDE. If it is SDE it checks if it has Edit priveleges. If it is non-SDE it checks user security policies which are applied based on the user identity and not uniformly to the file or directory. It also checks the workspace rights mode settings of the file or directory itself as shown before. I had to comment out the Threading interfaces and methods for checking the CurrentPrincipal because they threw errors, but I would have applied them if I could. If anyone knows how to get them to work in an Add-in, please let me know.Even if this code indicates that you can write to a dataset, you should still enclose any use of the Editor interface within a Try Catch block, since the file system is dynamic. You should assume that it is possible for settings to change during your process in ways that may cause a failure based on the inability to write to the dataset even after this test is passed, but normally this test should be a good indicator that the dataset can be written to immediately after the test passes. For SDE data you also still need to test if the Editor Option property for versioned/unversioned editing under IEditProperties3 matches the dataset's versioned state. You also need to make sure that no edit session is already in progress before attempting to begin a new edit session using the IEditor interface. Public Function CanWriteTo(ByVal pDataset As IDataset) As Boolean
If Not pDataset Is Nothing Then
' Dataset is valid
Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace = pDataset.Workspace
If Not workspace Is Nothing Then
' Workspace is valid
If workspace.Type = esriWorkspaceType.esriRemoteDatabaseWorkspace Then
' workspace is an SDE Worksapce.
' Verify editing privileges have been granted for the SDE data
Dim pFCName As IFeatureClassName = pDataset.FullName
Dim pSQLPriv As ISQLPrivilege = pFCName
If (pSQLPriv.SQLPrivileges And (esriSQLPrivilege.esriSelectPrivilege Or esriSQLPrivilege.esriUpdatePrivilege Or esriSQLPrivilege.esriInsertPrivilege Or esriSQLPrivilege.esriDeletePrivilege)) = (esriSQLPrivilege.esriSelectPrivilege Or esriSQLPrivilege.esriUpdatePrivilege Or esriSQLPrivilege.esriInsertPrivilege Or esriSQLPrivilege.esriDeletePrivilege) Then
pSQLPriv = Nothing
pFCName = Nothing
workspace = Nothing
Return True
End If
Else
' workspace is Not an SDE Workspace
Dim DirectoryPath As String = workspace.PathName
Dim dirInfo As System.IO.DirectoryInfo = FileIO.FileSystem.GetDirectoryInfo(DirectoryPath)
Dim currentUser As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
' Dim currentPrinciple As System.Security.Principal.WindowsPrincipal = System.Threading.Thread.CurrentPrincipal
Dim acl As System.Security.AccessControl.AuthorizationRuleCollection = dirInfo.GetAccessControl().GetAccessRules(True, True, GetType(System.Security.Principal.SecurityIdentifier))
Dim currentRule As System.Security.AccessControl.FileSystemAccessRule
Dim bDenyWrite As Boolean = False
Dim bAllowWrite As Boolean = False
For x As Integer = 0 To acl.Count - 1
currentRule = acl(x)
If currentUser.User.Equals(currentRule.IdentityReference) Then ' Or currentPrinciple.IsInRole(currentRule.IdentityReference) Then
If currentRule.AccessControlType.Equals(Security.AccessControl.AccessControlType.Deny) Then
If (currentRule.FileSystemRights And Security.AccessControl.FileSystemRights.Write) = Security.AccessControl.FileSystemRights.Write Then bDenyWrite = True
If (currentRule.FileSystemRights And Security.AccessControl.FileSystemRights.Read) = Security.AccessControl.FileSystemRights.Read Then bDenyWrite = True
ElseIf (currentRule.FileSystemRights And (Security.AccessControl.FileSystemRights.Read Or Security.AccessControl.FileSystemRights.Write)) = (Security.AccessControl.FileSystemRights.Read Or Security.AccessControl.FileSystemRights.Write) Then
bAllowWrite = True
End If
End If
Next
Dim workspaceProperties As IWorkspaceProperties = TryCast(workspace, IWorkspaceProperties)
If workspaceProperties Is Nothing Then
Throw New ArgumentException("Workspace does not implement IWorkspaceProperties.")
End If
Dim workspaceProperty As IWorkspaceProperty = workspaceProperties.Property _
(esriWorkspacePropertyGroupType.esriWorkspacePropertyGroup, _
CInt(esriWorkspacePropertyType.esriWorkspacePropIsReadonly))
' See if the property is supported.
Dim bReadOnly As Boolean = False
If workspaceProperty.IsSupported Then
' Get the property value.
bReadOnly = CType(workspaceProperty.PropertyValue, Boolean)
End If
workspaceProperty = workspaceProperties.Property _
(esriWorkspacePropertyGroupType.esriWorkspacePropertyGroup, _
CInt(esriWorkspacePropertyType.esriWorkspacePropCanEdit))
Dim bCanEdit As Boolean = False
If workspaceProperty.IsSupported Then
' Get the property value.
bCanEdit = CType(workspaceProperty.PropertyValue, Boolean)
End If
workspaceProperty = Nothing
workspaceProperties = Nothing
currentRule = Nothing
currentUser = Nothing
dirInfo = Nothing
workspace = Nothing
If bAllowWrite And Not (bDenyWrite) And Not (bReadOnly) And bCanEdit Then
Return True
End If
End If
End If
End If
Return False
End Function
The function can be called using the code below: Dim bCanWrite As Boolean = CanWriteTo(pDataset) ' Assume pDataset is an IDataset variable. It may be assigned or unassigned, SDE or non-SDE.
If Not bCanWrite Then
MsgBox("The Workspace Does Now Allow Write Access!", MsgBoxStyle.Exclamation, "No Write Access To Workspace!")
Exit Sub ' one possible action for handling the inability to write to the dataset
Else
If Not pDataset Is Nothing Then
Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace = pDataset.Workspaceaset
If Not workspace Is Nothing Then
If workspace.Type = esriWorkspaceType.esriRemoteDatabaseWorkspace Then
Dim pVO3 As IVersionedObject3 = pDataset
Dim pEP3 As IEditProperties3 = m_Editor ' assumes this variable is already assigned with an instance of IEditor
Dim MESM As esriMultiuserEditSessionMode = pEP3.MultiuserEditSessionMode
If MESM = esriMultiuserEditSessionMode.esriMESMVersioned And Not pVO3.IsRegisteredAsVersioned Then
' Do something when the Edit Mode in set to versioned editing but the Dataset is non-versioned
ElseIf MESM = esriMultiuserEditSessionMode.esriMESMNonVersioned And pVO3.IsRegisteredAsVersioned Then
' Do something when the Edit Mode in set to non-versioned editing but the Dataset is versioned
End If
End If
' If IEditor is not already editing you may begin an IEditor edit session here if all the tests above passed and you resolved any mismatch in version edit mode
' You should still use a Try Catch block around your edit session.
End If
End If
End If