Posts Tagged version
Create Computer Account in Active Directory and add it to Domain with C#
Posted by Kelly's Chronicles in .NET, C#, Computers and Internet on July 10, 2014
I was asked to convert this code from my original post to C# by a friend who obviously didn’t want to do it himself. There was one conversion issue along the way I also fixed which is why I suspect he didn’t convert it himself. In other news, my assimilation into Memphis, TN is going well. I am slowly becoming accustomed to the heat and humidity and am trying to fight off the urge to eat everything in sight because the food is just amazing if a tad bit unhealthy. I finally am getting my car fixed and am looking forward to getting out and seeing this town for myself. Let’s hope it will be a lot of fun. Ok back to what you came here for. Here you go…..
DirectoryEntry Entry = new DirectoryEntry(“LDAP://” + ActiveDirectoryTree.SelectedNode.Name, strDomain + “\\” + strUser, strPass, AuthenticationTypes.Secure);
string ComputerName = null;
string CompSystem = null;
int ComputerCount = ComputerList.Items.Count;
int i = 0;
for (i = 0; i <= ComputerCount – 1; i++)
{
CompSystem = ComputerList.Items(i).Text;
DirectoryEntry ComputerEntry = Entry.Children.Add(“CN=” + CompSystem + “”, “Computer”);
ComputerEntry.Properties(“sAMAccountName”).Value = “” + CompSystem + “”;
ComputerEntry.CommitChanges();
int exp = Convert.ToInt32(ComputerEntry.Properties(“UserAccountControl”).Value);
ComputerEntry.Properties(“UserAccountControl”).Value = exp | 0x1;
ComputerEntry.CommitChanges();
int val = Convert.ToInt32(ComputerEntry.Properties(“UserAccountControl”).Value);
ComputerEntry.Properties(“UserAccountControl”).Value = val & ~0x2;
ComputerEntry.CommitChanges();
ComputersCreated.Text = ComputersCreated.Text + CompSystem + Environment.NewLine;
ComputerEntry.Close();
ComputerEntry.Dispose();
}
ComputersCreated.Text = ComputersCreated.Text + “——————————” + Environment.NewLine;
Entry.Close();
Entry.Dispose();
and to add to the domain:
string[] args = new string[5];
string[] args2 = new string[3];
// The one difference from the vb.net version. Commented this declaration since looping variables in ‘foreach’
//loops are declared in the ‘foreach’ header in C#:
// ManagementObject comp = null;
ManagementObjectCollection comps = null;
ManagementClass clsComps = new ManagementClass(“Win32_ComputerSystem”);
comps = clsComps.GetInstances();
foreach (ManagementObject comp in comps)
{
// this == used to join the domain
args[0] = “DOMAIN to join”;
args[1] = “Password”;
args[2] = “User with domain privs”;
args[3] = “Specify OU Here (ou=test,dc=domain,dc=com)”;
args[4] = “1”;
UInt32 retVal = 0;
retVal = comp.InvokeMethod(“JoinDomainOrWorkgroup”, args);
retval2 = Convert.ToString(retVal);
if (retval2 == “0”)
{
MessageBox.Show(“Welcome to the domain”);
Close();
}
else
{
End If
}
.NET, Account, Active, ActiveDirectoryTree, assimilation, AuthenticationTypes, c#, Children, Close, clsComps, CommitChanges, CompSystem, computer, ComputerCount, ComputerEntry, ComputerList, ComputerName, Convert, Count, Create, declaration, difference, Directory, DirectoryEntry, Dispose, Domain, Entry, environment, Facebook, food, foreach, friend, GetInstances, header, Here, InvokeMethod, items, Join, JoinDomainOrWorkgroup, LDAP, ManagementClass, ManagementObject, ManagementObjectCollection, Memphis, MessageBox, Name, NewLine, news, Password, properties, retVal, Secure, SelectedNode, Specify, Text, user, UserAccountControl, Value, vb.net, version, Welcome
Show All Forms In A Project with C#
Posted by Kelly's Chronicles in .NET, C# on August 6, 2011
This will list all forms or other types you specify that are in your particular Visual Studio project. It is a continuation of redoing code I have previously done in vb.net for C#. It can come in handy when documenting your code during your project. If it’s a form, do a certain set of actions, a class then do another set of actions and so on.
You can find the vb.net version here. There were some changes made from that version.
Have a great day!
static void GETALLFORMSINPROJECT()
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetEntryAssembly();
Type[] Types = myAssembly.GetTypes();
foreach (Type myType in Types)
{
if (myType.BaseType == null) continue;
if (myType.BaseType.FullName == “System.Windows.Forms.Form”) MessageBox.Show(myType.Name);
//else { MessageBox.Show(myType.Name); }
}
}
.NET, .NET Framework, Assembly, BaseType, csharp, Form, Forms, FullName, GetTypes, MessageBox, Name, Project, Reflection, System, translation, Type, Types, vb.net, version, Visual Studio, Windows
Tag Generator for Live Writer: Problems and Workarounds
Posted by Kelly's Chronicles in Live Writer on July 31, 2011
One topic of emails and questions I receive are in relation to the Tag Generator, a plugin available for download, for Live Writer, a blog writing tool.
First some history. The Tag Generator was originally written over two years ago to provide myself a way to automatically generate tags for my blog posts. I got tired of doing it manually. It worked well for me so I decided to create a plugin that others could use. It uses the Microsoft Word thesaurus to verify words in the blog and their meanings. It isn’t perfect but its better than nothing. Obviously some of you agreed as over 14,000 downloads took place. However over the last couple of years, for various reasons, no work was done on this plugin and a new version of Live Writer was released. While the Tag Generator plugin still works, it does not support the options feature for plugins that came out with this version of Live Writer. In addition, as you all know, Windows Live Spaces no longer supports blogging, instead choosing to support the Word Press blogging resources. Generating these tags are obviously pointless.
You might wonder to yourself why I just don’t sit down and rewrite the plugin and be done with it. Basically it is because I am too busy at the moment to do so. I will at some point I promise. I apologize to those of you who have been frustrated. But this blog post today is to tell you how I still make use of this plugin and how you can too.
First after you are done writing your post, right click on your document and click select all.
Then click “Insert” in the Live Writer toolbar and click “Generate Tags”.
Click the option or options you wish to generate the tags for and click the button “Generate”
Add, modify or delete the tags you wish…..but do not click the Insert button at the bottom! Instead select all of the words in the “Tags” text box and copy them. Then in the Set tags text box at the top of Live Writer, paste these words in their textbox.
Now your post will be tag ready for publication whenever you choose!
I am sorry for the hassle. I still find this plugin extremely useful for me and I promise I will get around to redoing it for you at some point. But this works fine for me for now and hopefully will for you as well.
addition, blog, Click, Generate, generator, history, Insert, Live, Microsoft, moment, option, options, plugin, Problems, publication, relation, resources, Spaces, Tag, Tags, Text, thesaurus, tool, topic, version, Windows, Word, Workarounds, Writer
Write Microsoft Word Document Header and Footer with C#
Posted by Kelly's Chronicles in .NET, C# on July 30, 2011
Again I am rewriting some of the more popular entries in this blog as C#. It’s good exercise for me and good for you because you get the code another way. Well at least that’s the theory! This entry writes to the header and footer of a document in Microsoft Word The vb.net version was here. The only downside to all of this is that this is only tested against Office 2010. The old version I had Office 2000, XP and 2003 on my machine. Essentially however it is the same code. If you have any questions of course please feel free to leave a comment or email me.
public void WriteHeaderandFooterinWordDocument()
{
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;// = Convert.ToInt32(Word.WdSeekView.wdSeekCurrentPageHeader);
oDoc.Content.Application.Selection.TypeText(“Martens “);
oDoc.Content.Application.Selection.Fields.Add(oDoc.Content.Application.Selection.Range, Word.WdFieldType.wdFieldEmpty, “PAGE”);
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
oDoc.Content.Application.Selection.TypeText(“Martens”);
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}
.NET, .NET Framework, ActivePane, Application, Content, Convert, csharp, Document, Facebook, Fields, Footer, header, Machine, Martens, Microsoft, Office, PAGE, Range, SeekView, Selection, TypeText, vb.net, version, View, Visible, WdFieldType, wdSeekCurrentPageHeader, WdSeekView, Word, Write
Recent Comments
Archives
- April 2018
- January 2018
- December 2017
- June 2017
- May 2017
- May 2016
- April 2016
- September 2015
- July 2014
- June 2014
- November 2012
- October 2012
- June 2012
- May 2012
- April 2012
- January 2012
- September 2011
- August 2011
- July 2011
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- May 2008
Get or Write Image Metadata with vb.net
Posted by Kelly's Chronicles in .NET, vb.net on September 1, 2011
I recently started a new contract with a local company and their project is quite image intensive. One of the tasks set before me was to store custom data in an image. Things such as the title, comments and keywords. Turned out to be quite an extensive undertaking. With the help of some other sources, I came up with this code. Ironically as it turned out we can’t use my solution because of some issues with custom image formats that are not respected in Microsoft Windows. But it is here for you! I will post the C# version soon. Also pay attention to your encoding. This was an issue that tripped me up here for a while.
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Reflection
Imports System.IO
Public Class clsReadMetaData
Public Function ReadEXIFMetadata(ByVal filepath As String) As ImageMetadata
Dim fs As New FileStream(filepath, FileMode.Open, FileAccess.Read)
Dim image__1 As Image = Image.FromStream(fs)
Dim imagePropertyItems As PropertyItem() = image__1.PropertyItems
Dim imageMetadata As New ImageMetadata()
For Each pi As PropertyItem In imagePropertyItems
Select Case CType(pi.Id, EXIFProperty)
Case EXIFProperty.Title
imageMetadata.Title = Encoding.Unicode.GetString(pi.Value)
‘imageMetadata.Title = Encoding.UTF32.GetString(pi.Value)
Exit Select
Case EXIFProperty.Author
imageMetadata.Author = Encoding.Unicode.GetString(pi.Value)
‘imageMetadata.Author = Encoding.UTF8.GetString(pi.Value)
Exit Select
Case EXIFProperty.Keywords
imageMetadata.Keywords = Encoding.Unicode.GetString(pi.Value)
‘imageMetadata.Keywords = Encoding.UTF8.GetString(pi.Value)
Exit Select
Case EXIFProperty.Comments
imageMetadata.Comments = Encoding.Unicode.GetString(pi.Value)
‘imageMetadata.Comments = Encoding.UTF8.GetString(pi.Value)
Exit Select
Case Else
Exit Select
End Select
Next
fs.Close()
Return imageMetadata
End Function
Public Sub SaveEXIFMetadata(ByVal image As Image, ByVal metadata As ImageMetadata, ByVal filepath As String)
SaveEXIFMetadataProperty(image, EXIFProperty.Title, metadata.Title, filepath)
SaveEXIFMetadataProperty(image, EXIFProperty.Author, metadata.Author, filepath)
SaveEXIFMetadataProperty(image, EXIFProperty.Keywords, metadata.Keywords, filepath)
SaveEXIFMetadataProperty(image, EXIFProperty.Comments, metadata.Comments, filepath)
End Sub
Private Sub SaveEXIFMetadataProperty(ByVal image As Image, ByVal [property] As EXIFProperty, ByVal propertyValue As String, ByVal filepath As String)
Dim propertyItem As PropertyItem = CreatePropertyItem()
propertyItem.Id = CInt([property])
‘ Type=1 means Array of Bytes.
propertyItem.Type = 2
propertyItem.Len = propertyValue.Length
‘propertyItem.Value = Encoding.Unicode.GetBytes(propertyValue)
propertyItem.Value = Encoding.UTF8.GetBytes(propertyValue)
image.SetPropertyItem(propertyItem)
image.Save(filepath)
End Sub
Private Function CreatePropertyItem() As PropertyItem
Dim ci As System.Reflection.ConstructorInfo = GetType(PropertyItem).GetConstructor(BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.[Public], Nothing, New Type() {}, Nothing)
Return DirectCast(ci.Invoke(Nothing), PropertyItem)
End Function
End Class
Public Enum EXIFProperty
Title = 40091
Author = 40093
Keywords = 40094
Comments = 40092
End Enum
Public Class ImageMetadata
Private _title As String = String.Empty
Private _author As String = String.Empty
Private _keywords As String = String.Empty
Private _comments As String = String.Empty
Public Sub New()
Me._title = String.Empty
Me._author = String.Empty
Me._keywords = String.Empty
Me._comments = String.Empty
End Sub
Public Sub New(ByVal title As String, ByVal author As String, ByVal keywords As String, ByVal comments As String)
Me._title = title
Me._author = author
Me._keywords = keywords
Me._comments = comments
End Sub
Public Property Title() As String
Get
Return Me._title
End Get
Set(ByVal value As String)
Me._title = value
End Set
End Property
Public Property Author() As String
Get
Return Me._author
End Get
Set(ByVal value As String)
Me._author = value
End Set
End Property
Public Property Keywords() As String
Get
Return Me._keywords
End Get
Set(ByVal value As String)
Me._keywords = value
End Set
End Property
Public Property Comments() As String
Get
Return Me._comments
End Get
Set(ByVal value As String)
Me._comments = value
End Set
End Property
End Class
.NET Framework, Array, author, BindingFlags, Bytes, Case, Class, Close, Collections, Comments, ConstructorInfo, CreatePropertyItem, custom, Data, DirectCast, Enum, FileMode, filepath, FileStream, FromStream, FUNCTION, Generic, GetBytes, GetConstructor, GetType, Image, ImageMetadata, imagePropertyItems, Imports, instance, Keywords, Length, Metadata, Microsoft, NonPublic, Open, Private, PropertyItem, PropertyItems, propertyValue, Public, Read, ReadEXIFMetadata, Reflection, Return, Save, Select, SetPropertyItem, solution, System, tasks, Text, Title, Type, Unicode, Value, vb.net, version, Windows, Write, _author, _comments, _keywords, _title
8 Comments