I need to write the results of 2 combo boxes to a text file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
When I click the ComboBoxGroups groups (a list of groups) the DataGridViewUsers populates a list of users in the selected group,
At the same time I click the the <span style="font-size:12pt; font-family:Times New Roman,serif
ComboBoxGroups groups I would like to write the result of the ComboBoxGroups and DataGridViewUsers to text file

Or add a command button and wrirw the results of
the DataGridViewUsers and DataGridViewUsers and DV to a text file

<pre class="prettyprint lang-vb Imports System
Imports System.DirectoryServices.AccountManagement
Imports System.Data.SqlClient
Imports System.Data
Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
populate comboboxs

Dim DS As New DataSet
Dim Bsource As New BindingSource
Dim DS2 As New DataSet
Dim Bsource2 As New BindingSource
Dim cUser As String = System.DirectoryServices.AccountManagement.UserPrincipal.Current.Name
DS = getallusers()
DS2 = getallgroups()
Bsource.DataSource = DS
Bsource.DataMember = DS.Tables("UserList").TableName
Bsource2.DataSource = DS2
Bsource2.DataMember = DS2.Tables("GroupList").TableName
comboUsers.DataSource = Bsource
comboUsers.DisplayMember = "FullName"
comboUsers.ValueMember = "UserName"
ComboBoxGroups.DataSource = Bsource2
ComboBoxGroups.DisplayMember = "GroupName"
ComboBoxGroups.ValueMember = "GroupName"

try going to current user
comboUsers.SelectedValue = cUser
End Sub
Private Sub comboUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboUsers.SelectedIndexChanged
populate gridview
Dim ds As New DataSet
Dim uname As String = comboUsers.SelectedValue.ToString
ds = GetGroupsDS(uname)
DataGridViewUsers.Visible = False
DataGridViewGroups.DataSource = ds
DataGridViewGroups.DataMember = ds.Tables("Groups").TableName
DataGridViewGroups.Columns(0).Width = 200
Call writeuserstofile()
DataGridViewGroups.Visible = True
End Sub
Public Shared Function getUserfullname() As String
Dim userfullname As String
Dim commapoint As Integer
Dim lastname As String
Dim firstname As String
userfullname = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
commapoint = InStr(userfullname, ", ")
If commapoint > 0 Then
lastname = Microsoft.VisualBasic.Left(userfullname, commapoint - 1)
firstname = Microsoft.VisualBasic.Right(userfullname, Len(userfullname) - (commapoint + 1))
Return firstname & " " & lastname
Else
Return userfullname
End If
End Function
Public Shared Function getGroups() As String
Dim adgroup As String
Dim grouptext As String
adgroup = System.Configuration.ConfigurationManager.AppSettings("ADgroup") "SQL_Carsons"
grouptext = ""

For Each group As System.Security.Principal.IdentityReference In System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups
grouptext = grouptext & group.Translate(GetType(System.Security.Principal.NTAccount)).ToString() & "<br>"
Next
If InStr(grouptext, adgroup) > 0 Then
Return True
Else
Return False
End If
getGroups = grouptext
getGroups = GetGroupsString("blairhe")
End Function
Public Function GetGroupsNew(ByVal userName As String) As List(Of GroupPrincipal)
Dim result As New List(Of GroupPrincipal)()
establish domain context
Dim yourDomain As New PrincipalContext(ContextType.Domain)
find your user
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(yourDomain, userName)
if found - grab its groups
If user IsNot Nothing Then
Dim groups As PrincipalSearchResult(Of Principal) = user.GetAuthorizationGroups()
iterate over all groups
For Each p As Principal In groups
make sure to add only group principals
If TypeOf p Is GroupPrincipal Then
result.Add(DirectCast(p, GroupPrincipal))
End If
Next
End If
Return result
End Function
Public Shared Function GetGroupsString(ByVal userName As String) As String List(Of GroupPrincipal)
Dim result As String = "" New List(Of GroupPrincipal)()
establish domain context
GetGroupsString = ""
Dim yourDomain As New PrincipalContext(ContextType.Domain)
find your user
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(yourDomain, userName)
if found - grab its groups
If user IsNot Nothing Then
Dim groups As PrincipalSearchResult(Of Principal) = user.GetAuthorizationGroups()
iterate over all groups
For Each p As Principal In groups
make sure to add only group principals
If TypeOf p Is GroupPrincipal Then
GetGroupsString = GetGroupsString & (DirectCast(p, GroupPrincipal).ToString)
End If
Next
End If
End Function
Public Shared Function GetGroupsDS(ByVal userName As String) As DataSet List(Of GroupPrincipal)


Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim dt2 As DataTable = New DataTable()
Dim dr As DataRow
ds.Tables(0).Columns.Add("ADGroup")
dt.Columns.Add("ADGroup")
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
Dim yourDomain As New PrincipalContext(ContextType.Domain)
find your user
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(yourDomain, userName)
if found - grab its groups
If user IsNot Nothing Then
Dim groups As PrincipalSearchResult(Of Principal) = user.GetAuthorizationGroups()
iterate over all groups
For Each p As Principal In groups
make sure to add only group principals
If TypeOf p Is GroupPrincipal Then
GetGroupsString = GetGroupsString() & (DirectCast(p, GroupPrincipal).ToString)
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
dr("ADGroup") = (DirectCast(p, GroupPrincipal).ToString)
ds.Tables(0).Rows.Add(dr)
dt.Rows.Add(dr)
End If
Next
End If
dt.DefaultView.Sort = "ADGroup"
dt2 = dt.DefaultView.ToTable("Groups")
ds.Merge(dt2)
Return ds
End Function
Public Shared Function getallusers() As DataSet
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim dt2 As DataTable = New DataTable()
Dim dr As DataRow
ds.Tables(0).Columns.Add("ADGroup")
dt.Columns.Add("UserName")
dt.Columns.Add("FullName")
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
Dim yourDomain As New PrincipalContext(ContextType.Domain)
Dim insPrincipalSearcher As New PrincipalSearcher()
Dim insUserPrincipal As New UserPrincipal(yourDomain)
Dim userstring As String = ""
Dim namestring As String = ""
insUserPrincipal.Name = "*"
insPrincipalSearcher.QueryFilter = insUserPrincipal
Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll()
For Each p As Principal In results
dr = dt.NewRow()
userstring = p.Name.ToString
If p.DisplayName IsNot Nothing Then
namestring = p.DisplayName.ToString
Else
namestring = p.Name.ToString
End If
lbUsers.Items.Add(p)
dr("UserName") = userstring
dr("FullName") = namestring
dt.Rows.Add(dr)
Next
getallusers = userstring
dt.DefaultView.Sort = "FullName"
dt2 = dt.DefaultView.ToTable("UserList")
ds.Merge(dt2)
Return ds
Return userstring
End Function
Public Shared Function getallgroups() As DataSet
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim dt2 As DataTable = New DataTable()
Dim dr As DataRow
ds.Tables(0).Columns.Add("ADGroup")
dt.Columns.Add("GroupName")
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
Dim yourDomain As New PrincipalContext(ContextType.Domain)
Dim insPrincipalSearcher As New PrincipalSearcher()
Dim insGroupPrincipal As New GroupPrincipal(yourDomain)
Dim groupstring As String = ""
insGroupPrincipal.Name = "*"
insPrincipalSearcher.QueryFilter = insGroupPrincipal
Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll()
For Each p As Principal In results
dr = dt.NewRow()
groupstring = p.Name.ToString
lbUsers.Items.Add(p)
dr("GroupName") = groupstring
dt.Rows.Add(dr)
Next
getallusers = userstring
dt.DefaultView.Sort = "GroupName"
dt2 = dt.DefaultView.ToTable("GroupList")
ds.Merge(dt2)
Return ds
Return userstring
End Function
Public Shared Function GetUsersDS(ByVal GroupName As String) As DataSet List(Of GroupPrincipal)
Dim result As String = "" New List(Of GroupPrincipal)()
establish domain context
GetGroupsString = ""
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim dt2 As DataTable = New DataTable()
Dim dr As DataRow
ds.Tables(0).Columns.Add("ADGroup")
dt.Columns.Add("ADUser")
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
Dim yourDomain As New PrincipalContext(ContextType.Domain)
find your group
Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(yourDomain, GroupName)
if found - grab its members
If group IsNot Nothing Then
Dim users As PrincipalSearchResult(Of Principal) = group.GetMembers()
iterate over all members
For Each p As Principal In users
If TypeOf p Is UserPrincipal Or TypeOf p Is GroupPrincipal Then
GetGroupsString = GetGroupsString() & (DirectCast(p, GroupPrincipal).ToString)
dr = ds.Tables(0).NewRow()
dr = dt.NewRow()
dr("ADUser") = p.Name.ToString
ds.Tables(0).Rows.Add(dr)
dt.Rows.Add(dr)
End If
Next
End If
dt.DefaultView.Sort = "ADUser"
dt2 = dt.DefaultView.ToTable("UserList")
ds.Merge(dt2)
Return ds
End Function
Public Shared Function NameParse(ByVal fullname As String) As String
Dim commapoint As Integer
Dim lastname As String
Dim firstname As String

commapoint = InStr(fullname, ", ")
If commapoint > 0 Then
lastname = Microsoft.VisualBasic.Left(fullname, commapoint - 1)
firstname = Microsoft.VisualBasic.Right(fullname, Len(fullname) - (commapoint + 1))
Return firstname & " " & lastname
Else
Return fullname
End If
End Function
Protected Sub ddlUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUsers.SelectedIndexChanged
Dim uname As String = ddlUsers.SelectedValue
lblGroups.Text = "hi!"
GridView2.Visible = False
GridView1.Caption = "AD Groups for " & uname
GridView1.DataSource = GetGroupsDS(uname)
GridView1.DataBind()
GridView1.Visible = True
End Sub
Protected Sub ddlGroups_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroups.SelectedIndexChanged
Dim groupname As String = ddlGroups.SelectedValue
lblGroups.Text = "hi!"
GridView1.Visible = False
GridView2.Caption = "AD Users for " & groupname
GridView2.DataSource = GetUsersDS(groupname)
GridView2.DataBind()
GridView2.Visible = True
End Sub

Private Sub ComboBoxGroups_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxGroups.SelectedIndexChanged
Dim ds As New DataSet
Dim gname As String = ComboBoxGroups.SelectedValue.ToString
ds = GetUsersDS(gname)
DataGridViewGroups.Visible = False
DataGridViewUsers.DataSource = ds
DataGridViewUsers.DataMember = ds.Tables("UserList").TableName
DataGridViewUsers.Columns(0).Width = 200
DataGridViewUsers.Visible = True
End Sub
Private Sub DataGridViewGroups_selectionchanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridViewGroups.SelectionChanged
Dim gname As String = ""


gname = DataGridViewGroups.Rows(rownumb).Cells(0).Value.ToString
gname = DataGridViewGroups.CurrentCell.Value.ToString
ComboBoxGroups.SelectedValue = gname
TextBox1.Text = gname
End Sub
Public Sub writeuserstofile()
Dim fname As String = "c:carsonsADINFO.txt"
If System.IO.File.Exists(Name) = True Then
Dim objwriter As New System.IO.StreamWriter(fname)
For Each Str As String
objwriter.WriteLine(Str)
Next
objwriter.Close()
Else
File.CreateText(fname)
Dim objWriter As New System.IO.StreamWriter(fname)
For Each Str As String In getUserfullname() added
objWriter.WriteLine(Str)
Next
objWriter.Close()
End If
End Sub
Private Sub DataGridViewGroups_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewGroups.CellContentClick
End Sub
End Class[/code]
<br/>

ie>>> jim jones administrator
<hr class="sig james p mcfadden

View the full article
 
Back
Top