<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>API Programming &#187; Windows</title>
	<atom:link href="http://www.brangle.com/wordpress/tag/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brangle.com/wordpress</link>
	<description>Just another computer weblog</description>
	<lastBuildDate>Tue, 08 Sep 2009 05:41:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How To Use the OpenFileDialog Box in VB.NET</title>
		<link>http://www.brangle.com/wordpress/2009/09/howto-use-the-openfiledialog-box-in-vbnet/</link>
		<comments>http://www.brangle.com/wordpress/2009/09/howto-use-the-openfiledialog-box-in-vbnet/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 05:40:42 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=588</guid>
		<description><![CDATA[Using the OpenFileDialog box wasn't as intuitive as I initially expected, I was thinking I could just drag it out onto my form as a button and place it right where I wanted to.  But when I clicked on it from toolbox it just went into the bottom frame of my application, but not on my form like I was hoping for.  Below I is an example of a dynamically (aka created at run) created OpenFileDialog.  Because it's dynamically created when don't have to drag it out to  form, it's created by the code, not from the GUI.]]></description>
			<content:encoded><![CDATA[<p>Using the OpenFileDialog box wasn&#8217;t as intuitive as I initially expected, I was thinking I could just drag it out onto my form as a button and place it right where I wanted to.  But when I clicked on it from toolbox it just went into the bottom frame of my application, but not on my form like I was hoping for.  Below I is an example of a dynamically (aka created at run) created OpenFileDialog.  Because it&#8217;s dynamically created when don&#8217;t have to drag it out to  form, it&#8217;s created by the code, not from the GUI.<span id="more-588"></span></p>
<p>In this example, I put the OpenFileDialog in a function and not a subroutine.  The reason for this is that I want to return a value.  Functions allow you to return a value whereas a subroutine does not.</p>
<p>Let&#8217;s take a look at this screen shot, there are 3 main elements that we can set which I&#8217;ll describe in further detail.  They are the initial directory, filter, and filter index.</p>
<div id="attachment_594" class="wp-caption aligncenter" style="width: 576px"><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/09/OpenFileDialog.jpg" alt="OpenFileDialog" title="OpenFileDialog" width="566" height="483" class="size-full wp-image-594" /><p class="wp-caption-text">OpenFileDialog</p></div>
<p><b>Initial Directory</b> &#8211; I guess the easiest one to explain in the initial directory, when you open the dialog box you&#8217;ll typically want it to open to a particular folder, in most cases I would guess this to be the user&#8217;s <i>My Documents</i> folder.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">fileDialogBox.<span style="color: #0000FF;">InitialDirectory</span> <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">GetFolderPath</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">SpecialFolder</span>.<span style="color: #0000FF;">Personal</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>The above line does just that, we query the .NET class Environment to get the locations of the users My Documents folder. But now what if we wanted it to open up in the <i>C:\Program Files</i> folder? Simple enough&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">fileDialogBox.<span style="color: #0000FF;">InitialDirectory</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;C:\Program Files&quot;</span></pre></div></div>

<p><b>Filter</b> &#8211; This allows us to filter the filetypes/filenames that we want to see in the OpenFileDialog.  For example, if I want to only see *.txt I can create a text filter by assigning <i>fileDialogBox.Filter = Text Files (*.txt)|*.txt|</i> and literally we just repeat this pattern to add more file types.  Here&#8217;s a few more examples.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">fileDialogBox.<span style="color: #0600FF;">Filter</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Text Files (*.txt)|*.text|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;Word Documents (*.doc;*.docx)|*.doc;*.docx|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;boot.ini (boot.ini)|boot.ini|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;All text files beginning with the letter A (a*.txt)|a*.txt|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;All files beginning with accesslog* (accesslog*)|accesslog*|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;All Files(*.*)|&quot;</span></pre></td></tr></table></div>

<p><b>Filter Index</b> &#8211; To default to a particular filter you will need to select a filter index.  In this example you&#8217;ll notice that I have boot.ini selected.  We can do that by looking at the above piece of code and noticing that boot.ini comes 3rd in that list.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">fileDialogBox.<span style="color: #0000FF;">FilterIndex</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span></pre></div></div>

<p>That&#8217;s it, this allows us to keep our filters arranged alphabetically and still be able to select an item from the middle of the list.</p>
<p><b>The Code</b> &#8211; Here it is.  In the function <i>OpenFile()</i> it will return a string with the filename and path as a string.  If no filename is selected, a blank string is returned.  In the subroutine <i>btnOpenFile_Click</i> if we do have a file then I&#8217;ll just retrieve some of the file info and display it on a message box otherwise I&#8217;ll tell the user that they did not select a file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">'This code was developed by Gerardo Lopez and was downloaded from www.brangle.com</span>
<span style="color: #008080; font-style: italic;">'Complete source code is available at:</span>
<span style="color: #008080; font-style: italic;">'http://www.brangle.com/wordpress/2009/09/howto-use-the-openfiledialog-box-in-vbnet/</span>
&nbsp;
<span style="color: #008080; font-style: italic;">'This was declared as a function because I want to be able to return a filename</span>
<span style="color: #008080; font-style: italic;">'the filename will be returned as a string</span>
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Function</span> OpenFile<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span>
	<span style="color: #008080; font-style: italic;">'declare a string, this is will contain the filename that we return</span>
	<span style="color: #0600FF;">Dim</span> strFileName <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
	<span style="color: #008080; font-style: italic;">'declare a new open file dialog</span>
	<span style="color: #0600FF;">Dim</span> fileDialogBox <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> OpenFileDialog<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">'add file filters, this step is optional, but if you notice the screenshot</span>
	<span style="color: #008080; font-style: italic;">'above it does not look clean if you leave it off, I explained in further</span>
	<span style="color: #008080; font-style: italic;">'detail on my site how to add/modify these values</span>
	fileDialogBox.<span style="color: #0600FF;">Filter</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Rich Text Format (*.rtf)|*.rtf|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;Text Files (*.txt)|*.txt|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;Word Documents (*.doc;*.docx)|*.doc;*.docx|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;Image Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif|&quot;</span> _
	    <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;All Files(*.*)|&quot;</span>
	<span style="color: #008080; font-style: italic;">'this sets the default filter that we created in the line above, if you don't </span>
	<span style="color: #008080; font-style: italic;">'set a FilterIndex it will automatically default to 1</span>
	fileDialogBox.<span style="color: #0000FF;">FilterIndex</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>
	<span style="color: #008080; font-style: italic;">'this line tells the file dialog box what folder it should start off in first</span>
	<span style="color: #008080; font-style: italic;">'I selected the users my document folder</span>
	fileDialogBox.<span style="color: #0000FF;">InitialDirectory</span> <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">GetFolderPath</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">SpecialFolder</span>.<span style="color: #0000FF;">Personal</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">'Check to see if the user clicked the open button</span>
	<span style="color: #0600FF;">If</span> <span style="color: #000000;">&#40;</span>fileDialogBox.<span style="color: #0000FF;">ShowDialog</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> DialogResult.<span style="color: #0600FF;">OK</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
	    strFileName <span style="color: #008000;">=</span> fileDialogBox.<span style="color: #0000FF;">FileName</span>
	<span style="color: #008080; font-style: italic;">'Else</span>
	<span style="color: #008080; font-style: italic;">'   MsgBox(&quot;You did not select a file!&quot;)</span>
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">'return the name of the file</span>
	<span style="color: #FF8000;">Return</span> strFileName
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
&nbsp;
&nbsp;
<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> btnOpenFile_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> btnOpenFile.<span style="color: #0000FF;">Click</span>
	<span style="color: #008080; font-style: italic;">'declare a string to the filename</span>
	<span style="color: #0600FF;">Dim</span> strFileNameAndPath <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> OpenFile<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">'check to see if they selected a file or just clicked cancel</span>
	<span style="color: #0600FF;">If</span> <span style="color: #000000;">&#40;</span>strFileNameAndPath <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
	    <span style="color: #008080; font-style: italic;">'Chastise the user for not selecting a file :)</span>
	    <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;You did not select a file!&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #FF8000;">Else</span>
	    <span style="color: #008080; font-style: italic;">'Begin doing whatever it is you would normally do with the file!</span>
	    <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;You selected this file: &quot;</span> <span style="color: #008000;">&amp;</span> strFileNameAndPath <span style="color: #008000;">&amp;</span> vbCrLf <span style="color: #008000;">&amp;</span> _
		   <span style="color: #808080;">&quot;The filename is: &quot;</span> <span style="color: #008000;">&amp;</span> System.<span style="color: #0000FF;">IO</span>.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetFileName</span><span style="color: #000000;">&#40;</span>strFileNameAndPath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf <span style="color: #008000;">&amp;</span> _
		   <span style="color: #808080;">&quot;Located in: &quot;</span> <span style="color: #008000;">&amp;</span> System.<span style="color: #0000FF;">IO</span>.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetDirectoryName</span><span style="color: #000000;">&#40;</span>strFileNameAndPath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf <span style="color: #008000;">&amp;</span> _
		   <span style="color: #808080;">&quot;It has the following extension: &quot;</span> <span style="color: #008000;">&amp;</span> System.<span style="color: #0000FF;">IO</span>.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetExtension</span><span style="color: #000000;">&#40;</span>strFileNameAndPath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf <span style="color: #008000;">&amp;</span> _
		   <span style="color: #808080;">&quot;The file was created on &quot;</span> <span style="color: #008000;">&amp;</span> System.<span style="color: #0000FF;">IO</span>.<span style="color: #008000;">File</span>.<span style="color: #0000FF;">GetCreationTime</span><span style="color: #000000;">&#40;</span>strFileNameAndPath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> vbCrLf <span style="color: #008000;">&amp;</span> _
		   <span style="color: #808080;">&quot;The file was last written to on &quot;</span> <span style="color: #008000;">&amp;</span> System.<span style="color: #0000FF;">IO</span>.<span style="color: #008000;">File</span>.<span style="color: #0000FF;">GetLastWriteTime</span><span style="color: #000000;">&#40;</span>strFileNameAndPath<span style="color: #000000;">&#41;</span> _
	    <span style="color: #000000;">&#41;</span>
	<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/09/howto-use-the-openfiledialog-box-in-vbnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Cut/Copy/Paste into the Clipboard using VB.NET</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-cutcopypaste-text-into-clipboard-using-vb-net/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-cutcopypaste-text-into-clipboard-using-vb-net/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 04:45:56 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=527</guid>
		<description><![CDATA[In this example, I'll show you how to cut, copy, paste and clear text in the clipboard. Cutting and copying text is really straight forward, pasting text requires just a little bit of checking.  For example, we want to check to see what the clipboard contains before we try and do something like pasting an image into a text box.]]></description>
			<content:encoded><![CDATA[<p>In this example, I&#8217;ll show you how to cut, copy, paste and clear text in the clipboard. Cutting and copying text is really straight forward, pasting text requires just a little bit of checking.  For example, we want to check to see what the clipboard contains before we try and do something like pasting an image into a text box.<span id="more-527"></span></p>
<p>To copy text, its just a simple one-liner:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span>txtTextBox.<span style="color: #0000FF;">SelectedText</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>To cut text, it&#8217;s almost as simple as copying it, but we need to remember to delete the data from the text box once it&#8217;s been copied:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span>txtTextBox.<span style="color: #0000FF;">SelectedText</span><span style="color: #000000;">&#41;</span>
txtTextBox.<span style="color: #0000FF;">SelectedText</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span></pre></div></div>

<p>To paste text there&#8217;s several things we have to worry about like I mentioned above.  We first want to check what&#8217;s in the clipboard and verify that the contents of the clipboard can be pasted into whatever field/object we are trying to paste them into.  In this example, I&#8217;m going to use the GetText method to make sure I only get the text from the clipboard and paste it into the text field.  In the example below, I&#8217;ll get the data from the clipboard, then check to see if it is text before pasting it.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">txtTextBox.<span style="color: #0000FF;">SelectedText</span> <span style="color: #008000;">=</span> Clipboard.<span style="color: #0000FF;">GetText</span></pre></div></div>

<p><div id="attachment_529" class="wp-caption aligncenter" style="width: 228px"><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/CutCopyPasteApp.jpg" alt="Cut Copy Paste for .NET Applications" title="Cut Copy Paste for .NET Applications" width="218" height="140" class="size-full wp-image-529" /><p class="wp-caption-text">Cut Copy Paste for .NET Applications</p></div> Here are the names for each of the objects in the above photo, txtTextBox, btnCut, btnCopy, btnPaste each represent the text box, cut button, copy button, and paste button respectively.  I&#8217;ll use the same variable names in each programming language below.</p>
<h2>&nbsp; </h2>
<p><b>This is the code for the above program written in VB.NET</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">'This code was developed by Gerardo Lopez and was downloaded from www.brangle.com</span>
<span style="color: #008080; font-style: italic;">'Complete source code is available at:</span>
<span style="color: #008080; font-style: italic;">'http://www.brangle.com/wordpress/2009/08/how-to-cutcopypaste-text-into-clipboard-using-vb-net/</span>
&nbsp;
<span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> Form1
    <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> btnCut_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> btnCut.<span style="color: #0000FF;">Click</span>
        <span style="color: #008080; font-style: italic;">'Checks to see if the user selected anything</span>
        <span style="color: #0600FF;">If</span> txtTextBox.<span style="color: #0000FF;">SelectedText</span> &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
            <span style="color: #008080; font-style: italic;">'Good, the user selected something</span>
            <span style="color: #008080; font-style: italic;">'Copy the information to the clipbaord</span>
            Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span>txtTextBox.<span style="color: #0000FF;">SelectedText</span><span style="color: #000000;">&#41;</span>
            <span style="color: #008080; font-style: italic;">'Since this is a cut command, we want to clear whatever </span>
            <span style="color: #008080; font-style: italic;">'text they had selected when they clicked cut</span>
            txtTextBox.<span style="color: #0000FF;">SelectedText</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
        <span style="color: #FF8000;">Else</span>
            <span style="color: #008080; font-style: italic;">'If there was no text selected, print out an error message box</span>
            <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;No text is selected to cut&quot;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> btnCopy_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> btnCopy.<span style="color: #0000FF;">Click</span>
        <span style="color: #008080; font-style: italic;">'Checks to see if the user selected anything</span>
        <span style="color: #0600FF;">If</span> txtTextBox.<span style="color: #0000FF;">SelectedText</span> &lt;&gt; <span style="color: #808080;">&quot;&quot;</span> <span style="color: #FF8000;">Then</span>
            <span style="color: #008080; font-style: italic;">'Copy the information to the clipboard</span>
            Clipboard.<span style="color: #0000FF;">SetText</span><span style="color: #000000;">&#40;</span>txtTextBox.<span style="color: #0000FF;">SelectedText</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Else</span>
            <span style="color: #008080; font-style: italic;">'If no text was selected, print out an error message box</span>
            <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;No text is selected to copy&quot;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> btnPaste_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> btnPaste.<span style="color: #0000FF;">Click</span>
        <span style="color: #008080; font-style: italic;">'Get the data stored in the clipboard</span>
        <span style="color: #0600FF;">Dim</span> iData <span style="color: #FF8000;">As</span> IDataObject <span style="color: #008000;">=</span> Clipboard.<span style="color: #0000FF;">GetDataObject</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">'Check to see if the data is in a text format</span>
        <span style="color: #0600FF;">If</span> iData.<span style="color: #0000FF;">GetDataPresent</span><span style="color: #000000;">&#40;</span>DataFormats.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
            <span style="color: #008080; font-style: italic;">'If it's text, then paste it into the textbox</span>
            txtTextBox.<span style="color: #0000FF;">SelectedText</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span>iData.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">&#40;</span>DataFormats.<span style="color: #0000FF;">Text</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Else</span>
            <span style="color: #008080; font-style: italic;">'If it's not text, print a warning message</span>
            <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Data in the clipboard is not availble for entry into a textbox&quot;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></td></tr></table></div>

<p>Now in general, I don&#8217;t think that the clipboard should be cleared by the programmer, it should be cleared by the user.  But if you have a legitimate reason to clear the clipboard, here&#8217;s the code to do just that:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;">Clipboard.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-cutcopypaste-text-into-clipboard-using-vb-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Make Your Computer Talk with VB.NET Application (and Source Code)</title>
		<link>http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-application-and-source-code/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-application-and-source-code/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 02:57:25 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[XP]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=502</guid>
		<description><![CDATA[I was playing around with COM objects in Windows and discovered that I could access the SAPI.SpVoice interface and make Microsoft Windows talk to me.  Well needless to say I had to rush and develop and application to do just that.  I uploaded a simple applicaton which will just say whatever you type in as an argument.  For example <i>SpeakToMeConsole.exe Hello World</i> will make your computer speak the words "Hello World."  The part that really amazes me is that the guts of this program is only 2 lines long, and it can even that can reduced down to 1 line if we create an anonymous instance of SAPI.SpVoice object.]]></description>
			<content:encoded><![CDATA[<p>I was playing around with COM objects in Windows and discovered that I could access the SAPI.SpVoice interface and make Microsoft Windows talk to me.  Well needless to say I had to rush and develop and application to do just that.  I uploaded a simple applicaton which will just say whatever you type in as an argument.  For example <i>SpeakToMeConsole.exe Hello World</i> will make your computer speak the words &#8220;Hello World.&#8221;  The part that really amazes me is that the guts of this program is only 2 lines long, and it can even that can reduced down to 1 line if we create an anonymous instance of SAPI.SpVoice object.<span id="more-502"></span></p>
<p>The reason I created it as a console application and not a windows app is because I envisioned this program only really only having a valid function when it&#8217;s called from another application. Or even better, you want to spook your girlfriend or parents by making their laptop talk to them when it turns on!  Whatever your reasons are, enjoy! Oh and let me know how you use this program in the comments.</p>
<div id="attachment_506" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.brangle.com/downloads/SpeakToMe/v1.0/SpeakToMeConsole.exe"><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/download-button.jpg" alt="Download SpeakToMe App" title="Download SpeakToMe App" width="150" height="144" class="size-full wp-image-506" /></a><p class="wp-caption-text">Download SpeakToMe App</p></div>
<p>Here is the complete source code to the above application. Let me know what you think in the comments!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">'This application was developed by Gerardo Lopez and was downloaded from www.brangle.com</span>
<span style="color: #008080; font-style: italic;">'Complete source code is available at:</span>
<span style="color: #008080; font-style: italic;">'http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-app-source-code/</span>
&nbsp;
&nbsp;
Module Module1
    <span style="color: #008080; font-style: italic;">'We want to make sure we grab any arguments as args so that we speak them    </span>
    <span style="color: #0600FF;">Sub</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> args <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #008080; font-style: italic;">'If there are no arguments UBound will return -1</span>
        <span style="color: #008080; font-style: italic;">'Therefore we want to make sure to let the user know how to run the app if </span>
        <span style="color: #008080; font-style: italic;">'they don't enter any arguments</span>
        <span style="color: #0600FF;">If</span> <span style="color: #0600FF;">UBound</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span> &gt; <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span> <span style="color: #FF8000;">Then</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">'Generate an empty string, we'll use this to concatenate all of our arguments </span>
            <span style="color: #008080; font-style: italic;">'together so we can pass it to the narrator</span>
            <span style="color: #0600FF;">Dim</span> <span style="color: #FF8000;">input</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">'Concat all of our arguments together and make sure to put a space in </span>
            <span style="color: #008080; font-style: italic;">'between them so the words don't run together</span>
            <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> arg <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> In args
                <span style="color: #FF8000;">input</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">input</span> <span style="color: #008000;">+</span> arg <span style="color: #008000;">+</span> <span style="color: #808080;">&quot; &quot;</span>
            <span style="color: #FF8000;">Next</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">'Create an object and gain access to the narrator</span>
            <span style="color: #0600FF;">Dim</span> voice <span style="color: #008000;">=</span> <span style="color: #0600FF;">CreateObject</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SAPI.SpVoice&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #008080; font-style: italic;">'Tells the narrator what to say</span>
            voice.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">input</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Else</span>
            <span style="color: #008080; font-style: italic;">'The is where we tell the user what to say if they didn't enter any arguments</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;No arguments specified&quot;</span><span style="color: #000000;">&#41;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Try this instead: SpeakToMe.exe Hello World&quot;</span><span style="color: #000000;">&#41;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>vbCrLf <span style="color: #008000;">+</span> vbCrLf <span style="color: #008000;">+</span> <span style="color: #808080;">&quot;Visit www.brangle.com for other apps&quot;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
<span style="color: #0600FF;">End</span> Module
<span style="color: #008080; font-style: italic;">'The End</span></pre></td></tr></table></div>

<p>To run this program from the command line, you can type in something like the following:</p>
<pre>SpeakToMeConsole.exe Hello World</pre>
<p>And here is the beautiful one-liner!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">CreateObject</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SAPI.SpVoice&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Speak</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;hello from brangle.com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-application-and-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Shutdown, Restart, Log off your computer with VB.NET or C#</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-shutdown-restart-log-off-your-computer-with-vb-net-or-c/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-shutdown-restart-log-off-your-computer-with-vb-net-or-c/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 08:27:06 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Sever 2003]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[XP]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=458</guid>
		<description><![CDATA[This is extremely easy using VB.NET or C# (pronounced C sharp) and using the built in windows EXE file <i>shutdown.exe</i>.  To shutdown, restart, or log off your computer is as simple as entering 1 line into your program, it just so happens that the 1 line is identical whether you're using VB.NET or C#.]]></description>
			<content:encoded><![CDATA[<p>This is extremely easy using VB.NET or C# (pronounced C sharp) and the built in windows EXE file <i>shutdown.exe</i>.  To shutdown, restart, or log off your computer is as simple as entering 1 line into your program, it just so happens that the 1 line is identical whether you&#8217;re using VB.NET or C#.<span id="more-458"></span></p>
<p>To shutdown/turn off your computer, just execute the following line of code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">System.Diagnostics.Process.Start(<span style="color: #800000;">&quot;shutdown.exe&quot;</span>, <span style="color: #800000;">&quot;-s -t 0&quot;</span>)</pre></td></tr></table></div>

<p>To restart/reboot your computer, just execute the following line of code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">System.Diagnostics.Process.Start(<span style="color: #800000;">&quot;shutdown.exe&quot;</span>, <span style="color: #800000;">&quot;-r -t 0&quot;</span>)</pre></td></tr></table></div>

<p>To log off your computer, just execute the following line of code (and yes, that&#8217;s a lowercase L, not the number 1):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">System.Diagnostics.Process.Start(<span style="color: #800000;">&quot;shutdown.exe&quot;</span>, <span style="color: #800000;">&quot;-l -t 0&quot;</span>)</pre></td></tr></table></div>

<p>The number zero at the end of each of those examples is how long the computer should wait in seconds before shutting down/restarting/etc&#8230; In this example, I&#8217;m going to shutdown the computer in 5 minutes (5 minutes * 60 seconds = 300 seconds)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">System.Diagnostics.Process.Start(<span style="color: #800000;">&quot;shutdown.exe&quot;</span>, <span style="color: #800000;">&quot;-s -t 300&quot;</span>)</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-shutdown-restart-log-off-your-computer-with-vb-net-or-c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Create Message Box Pop Ups in VB.NET</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-create-message-box-pop-ups-in-vbnet/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-create-message-box-pop-ups-in-vbnet/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 07:25:00 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=431</guid>
		<description><![CDATA[Creating different types of messages boxes in Visual Basic is a simple one line statement.  I'm going to run through how to create several different types of message boxes in this tutorial, including OK Only, OK/Cancel, Abort/Retry/Ignore, and a Critical message box.]]></description>
			<content:encoded><![CDATA[<p>Creating different types of messages boxes in Visual Basic is a simple one line statement.  I&#8217;m going to run through how to create several different types of message boxes in this tutorial, including OK Only, OK/Cancel, Abort/Retry/Ignore, and a Critical message box.<span id="more-431"></span></p>
<p>The simplest message box, it only includes an OK button&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Hello World!&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/MsgBoxOnly.jpg" alt="MsgBoxOnly" title="MsgBoxOnly" width="161" height="100" class="alignnone size-full wp-image-434" /><br />
<br /><br /><br /></p>
<p>Ok, so the title of the message box the name of the project, let&#8217;s add our own title as the third argument, but first we&#8217;ll need to tell VB that we want to have this message box act as an OK only.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Hello World!&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">OkOnly</span>, <span style="color: #808080;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/MsgBoxOnlyWithTitle.jpg" alt="MsgBoxOnlyWithTitle" title="MsgBoxOnlyWithTitle" width="143" height="100" class="alignnone size-full wp-image-435" /><br />
<br /><br /><br /></p>
<p>The next message box is the OK/Cancel with a custom title.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Visit brangle.com?&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">OkCancel</span>, <span style="color: #808080;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/MsgBoxOKCancelWithTitle.jpg" alt="MsgBoxOKCancelWithTitle" title="MsgBoxOKCancelWithTitle" width="185" height="100" class="alignnone size-full wp-image-439" /><br />
<br /><br /><br /></p>
<p>OK great, but this time lets detect whether the user clicked OK or Cancel.  If the user clicks OK, We&#8217;ll launch my website <img src='http://www.brangle.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  otherwise we&#8217;ll do nothing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">If</span> <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Visit brangle.com?&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">OkCancel</span>, <span style="color: #808080;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> MsgBoxResult.<span style="color: #0600FF;">Ok</span> <span style="color: #FF8000;">Then</span>
     Process.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;http://www.brangle.com/&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></pre></td></tr></table></div>

<p><br /><br /><br /></p>
<p>Let&#8217;s try using an Abort/Retry/Ignore message box.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> result <span style="color: #FF8000;">As</span> DialogResult
result <span style="color: #008000;">=</span> <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Corrupt File. Try Again?&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">AbortRetryIgnore</span>, <span style="color: #808080;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> result <span style="color: #008000;">=</span> Windows.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">DialogResult</span>.<span style="color: #0600FF;">Abort</span> <span style="color: #FF8000;">Then</span>
   <span style="color: #008080; font-style: italic;">'Do some action to abort opening the file</span>
   <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;You clicked abort&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">ElseIf</span> result <span style="color: #008000;">=</span> Windows.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">DialogResult</span>.<span style="color: #0600FF;">Retry</span> <span style="color: #FF8000;">Then</span>
   <span style="color: #008080; font-style: italic;">'Do some action to retry opening the file</span>
   <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;You clicked retry&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #FF8000;">ElseIf</span> result <span style="color: #008000;">=</span> Windows.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">DialogResult</span>.<span style="color: #0600FF;">Ignore</span> <span style="color: #FF8000;">Then</span>
   <span style="color: #008080; font-style: italic;">'Do some action to ignore</span>
   <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;You clicked ignore&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></pre></td></tr></table></div>

<p><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/MsgBoxAbortRetryIgnore.jpg" alt="MsgBoxAbortRetryIgnore" title="MsgBoxAbortRetryIgnore" width="266" height="100" class="alignnone size-full wp-image-450" /><br />
<br /><br /><br /></p>
<p>Now let&#8217;s launch a message box as critical, this will display the red icon with x thru it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;File not found!&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">Critical</span>, <span style="color: #808080;">&quot;Error&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/MsgBoxCritical.jpg" alt="MsgBoxCritical" title="MsgBoxCritical" width="146" height="119" class="alignnone size-full wp-image-452" /><br />
<br /><br /><br /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-create-message-box-pop-ups-in-vbnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine or Join Two Text Files Using PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 03:28:37 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=422</guid>
		<description><![CDATA[Merging files together, or to a seperate file, is a snap with PowerShell using the <i>Get-Content</i> cmdlet.  If you've never used the Add-Content cmdlet, I would suggest first reading my other article <a href="/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/" target="_top">Append Text to a File Using Add-Content</a>.  The first thing we need to do is identify our two files, in my case I'll be using <i>file1.log</i> and <i>file2.log</i>.  In the first example, I append all the text of <i>file2.log</i> into the end of <i>file1.log</i>.  In the second example, I'll create a completely new file and dump the contents of both the files into the newly created file.  In the last example, I'll add the current date to the filename of a completly new log file.]]></description>
			<content:encoded><![CDATA[<p>Merging files together, or to a seperate file, is a snap with PowerShell using the <i>Get-Content</i> cmdlet.  If you&#8217;ve never used the Add-Content cmdlet, I would suggest first reading my other article <a href="/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/" target="_top">Append Text to a File Using Add-Content</a>.  The first thing we need to do is identify our two files, in my case I&#8217;ll be using <i>file1.log</i> and <i>file2.log</i>.  In the first example, I append all the text of <i>file2.log</i> into the end of <i>file1.log</i>.  In the second example, I&#8217;ll create a completely new file and dump the contents of both the files into the newly created file.  In the last example, I&#8217;ll add the current date to the filename of a completly new log file.<span id="more-422"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Now, what if we want to copy the contents of <i>file1.log</i> and <i>file2.log</i> into a completely new file called <i>file3.log</i></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800000;">&quot;file3.log&quot;</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>What if we want to erase everything in file3.log before we add new content? In this case, instead of creating a new file, we&#8217;ll use the Clear-Contents cmdlet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Clear-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Ok, well, what if I don&#8217;t know if file3.log already exists? If it doesn&#8217;t exist and I run the Clear-Contents cmdlet then I&#8217;ll get an error. And if it does exist and I try to create it as a new file I&#8217;ll still get an error.  There&#8217;s an easy way to do this, we&#8217;ll use the New-Item cmdlet from the example above, but we&#8217;ll add the -force argument, so even if the file exists it will overwrite it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #008080; font-style: italic;">-force</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Alright, so I guess I got the hang of this, now lastly since these are log files, how can I append the date to the file name? Simple, with the <i>Get-Date</i> cmdlet.  Let&#8217;s output our file name as YYYYMMDD-WebAccess.log in this example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$date</span><span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span>.ToString<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;yyyyMMdd&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$NewFileName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$date-WebAccess.log&quot;</span>
<span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800080;">$NewFileName</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$NewFileName</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$NewFileName</span> $file2</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Add or Map a Custom Hostnames to IP Address without a DNS Server</title>
		<link>http://www.brangle.com/wordpress/2009/08/add-or-map-custom-hostnames-to-ip-address-without-dns-server/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/add-or-map-custom-hostnames-to-ip-address-without-dns-server/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 07:32:14 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iTouch]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[Sever 2003]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[XP]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=400</guid>
		<description><![CDATA[There's been several times where I had an Apache server running locally on my computer as a development (or dev) server.  Now if this is on my home network, or if your system administrator won't give you your own custom host name then you have to go the DIY (do it yourself) route.  But, often times, I have to hard code host names, or I need several different websites hosted on my personal machine.  Well that's where the <i>hosts</i> file comes into play.]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been several times where I had an Apache server running locally on my computer as a development (or dev) server.  Now if this is on my home network, or if your system administrator won&#8217;t give you your own custom host name then you have to go the DIY (do it yourself) route.  But, often times, I have to hard code host names, or I need several different websites hosted on my personal machine.  Well that&#8217;s where the <i>hosts</i> file comes into play.<span id="more-400"></span></p>
<p>Another example of needing a custom ip address is at my company, I&#8217;ve needed to check the status of a printer and if I want to try the access it via its hostname <i>http://printername</i> it wont work because the SysAdmin has shared the printer on the network without adding a custom dns host name, even though it has a static IP address on the network. So I added the name of each printer to my <i>hosts</i> file so now I have no problem accessing any printer via its name, without having to first look up its IP address.<!--more--></p>
<p>Also, if you intend to <b>block a website by its hostname</b> then this is also the solution for you.</p>
<p>You can find the <i>hosts</i> file in the following folder&#8230;<br />
Windows 95, 98, and ME in the %WinDir%\ directory<br />
NT, 2000, XP, 2003, Vista, 7 in the %SystemRoot%\system32\drivers\etc\ directory (%SystemRoot% is almost always C:\Windows)<br />
iPhone and iTouch in the /private/etc/hosts directory<br />
Linux in the /etc/hosts directory</p>
<p>Also, a note for 0.1% of Windows NT, 2000, XP, 2003, Vista, and Windows 7 users, the actual location may differ, to find the real location you&#8217;ll need to dig into the registry key, give the following a look up</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="regedit" style="font-family:monospace;">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters</pre></td></tr></table></div>

<p> and look for the following string key <i>DataBasePath</i>.  On my computer it was declared as <i>%SystemRoot%\System32\drivers\etc</i>  I know that&#8217;s what I said it would be above, I just want all you SysAdmins to be aware of that 0.1% chance.</p>
<p>Lastly once, you finish making any modifications you&#8217;ll need to flush your locally cached DNS on your computer. </p>
<p>In WinXP you can edit the <i>c:\windows\system32\drivers\etc\hosts</i> file by opening it in notepad or any ascii text editor. By default the last line you&#8217;ll see is
<pre>127.0.0.1       localhost</pre>
<p> but other programs can change this.  The IP address 127.0.0.1 is always the ip address of the local computer.  It doesn&#8217;t matter if the computer has a different IP address on the network, 127.0.0.1 is considered a loop back address, in other words, it always points to your computer.</p>
<p>you can add a new hostname followed by the ip address in the text file that&#8217;s it, it&#8217;s just that simple!  Let&#8217;s add example.com to our local host names, now if we had apache installed, this ip address would always map to our computer just by adding the following line to the end of your <i>hosts</i> file&#8230; </p>
<pre>127.0.0.1    example.com</pre>
<p>Now what if you want to block a website like MySpace? Add the following line </p>
<pre>0.0.0.0       myspace.com</pre>
<p>The problem with this approach is it only blocks the hostname myspace.com, it leaves www.myspace.com and login.myspace.com completely accessible, and other hostname that I don&#8217;t know about (or worse yet, any future host names that they decide to add).  So let&#8217;s block all we we can by pointing them to a null IP address</p>
<pre>0.0.0.0       myspace.com
0.0.0.0       www.myspace.com
0.0.0.0       login.myspace.com</pre>
<p>Ok great, I added my login.myspace.com to the <i>hosts</i> file, but I can still access it, what gives?!? There is two possible fixes for this solution. First close all of your browsers, for example, if you can still access the domain in Internet Explorer or FireFox it&#8217;s because the IP address has been cached, close the browser so that the browser can &quot;forget&quot; the IP address.  Now before you reopen the application we also need to clear the DNS entry from Windows.  We can clear a DNS entry from windows by clicking <i>Start</i>, <i>Run</i> typing <i>ipconfig /flushdns</i> then clicking ok.  This will flush the ip address address stored by the operating system.  You should now by able to open FireFox and Internet Explorer and verify that they no longer have the IP address cached.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/add-or-map-custom-hostnames-to-ip-address-without-dns-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Retrieve File Permissions using Get-Acl with PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 06:30:24 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=385</guid>
		<description><![CDATA[Retrieving the owner and permissions of a file, folders and even registry keys is a breeze with PowerShell's Get-Acl cmdlet.  Let's try checking to see who has access to to the <i>C:\Documents and Settings\Administrator</i> folder.]]></description>
			<content:encoded><![CDATA[<p>Retrieving the owner and permissions of a file, folders and even registry keys is a breeze with PowerShell&#8217;s Get-Acl cmdlet.  Let&#8217;s try checking to see who has access to to the <i>C:\Documents and Settings\Administrator</i> folder.<span id="more-385"></span></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Documents and Settings\Administrator&quot;</span></pre></div></div>

<p> Now this return everything that I would expect it to, but I prefer the <i>Format-List</i> view, so let&#8217;s check the permissions on the the file <i>C:\Windows\clock.avi</i> and output it in a list view.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Windows\clock.avi&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-List</span></pre></div></div>

<p>Ok, now I did say we could check the permissions of a registry key, so let&#8217;s go for it&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> HKLM:\Software\Microsoft</pre></td></tr></table></div>

<p>Now if you just want to find out the owner of the file you could pull back just that value</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Windows\clock.avi&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select</span> Owner</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Ping a Computer using PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 04:22:19 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=380</guid>
		<description><![CDATA[Pinging a computer is really simple, and of course since this is PowerShell we don't want to just use something as primitive as <i>ping.exe</i>, we want to use .NET and take advantage of all the method calls and filters.]]></description>
			<content:encoded><![CDATA[<p>Pinging a computer is really simple, and of course since this is PowerShell we don&#8217;t want to use something as primitive as <i>ping.exe</i>, we want to use .NET and take advantage of all the method calls and filters.<span id="more-380"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.NetworkInformation.Ping
<span style="color: #800080;">$ping</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>We can also set a timeout so that our computer doesn&#8217;t hang to long on a server that won&#8217;t respond. Let&#8217;s add a timeout of a 5 seconds, we will first need to convert this to milliseconds, so that&#8217;s 5,000ms. Of course 5 seconds may not be a resonable timeout, you need to figure out what&#8217;s appropriate for you and what you&#8217;re willing to tolerate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.NetworkInformation.Ping
<span style="color: #800080;">$ping</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;www.brangle.com&quot;</span><span style="color: pink;">,</span> <span style="color: #000000;">5000</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Technical details: System.Net.NetworkInformation.Ping requires that you have .NET 2.0 or greater.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using the System.Environment Class in PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 08:24:58 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=369</guid>
		<description><![CDATA[The Environment class has a huge array of information and accessing this information is very simple, I'm going to try and give a whole bunch of one line examples of how to do this using PowerShell.]]></description>
			<content:encoded><![CDATA[<p>The Environment class has a huge array of information and accessing this information is very simple, I&#8217;m going to try and give a whole bunch of one line examples of how to do this using PowerShell.<span id="more-369"></span></p>
<p>To retrieve the comptuer name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::MachineName</pre></td></tr></table></div>

<p>Get Operating System Information (such as Platform, service pack, version, and version string)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::OSVersion</pre></td></tr></table></div>

<p>Checking to see if your computer has started to shutdown</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::HasShutdownStarted</pre></td></tr></table></div>

<p>Instead of trying to remember if you should use <i>`r`n</i> or is it just <i>`n</i>? A simple way is just to get .NET to tell you and assign this value to a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$nl</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::NewLine</pre></td></tr></table></div>

<p>Get the system directory (normally this is C:\Windows\System32), but for some computers it could be different</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::SystemDirectory</pre></td></tr></table></div>

<p>Get the user&#8217;s domain name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::UserDomainName</pre></td></tr></table></div>

<p>Get the user&#8217;s name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::UserName</pre></td></tr></table></div>

<p>Get a list of all environment variables</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariables<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Get a particular environment variable</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$path</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;path&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$temp</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;temp&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$os</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;os&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$homedrive</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;homedrive&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Create a new environment variable, or overwrite an existing one</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::SetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;VariableName&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;VariableValue&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Get all logical drives</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetLogicalDrives<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
