So for some reason you have the need to use vbscript (maybe a .vbs file, Excel script, etc) but like many others before you, you have run into a limitation of vbscript. I’m not going to discuss the reasons why / why not to use vbscript, I am already assuming you have a good reason. Onto the example. In this case, we are “fixing” the limitation of vbscript to not handle larger numbers well. We are going to extend our vbscript by creating a .dll library in Visual Studio that allows us to pass and return data in the format we need, in the example case, to round a large decimal number. Continue reading “Creating a .dll library for use in vbscript (.vbs) using Visual Studio”
.net
Downloading a remote file with vb.net / c# using WebClient
Can you believe it’s 2013 already? Me either. Anyhow, just wanted to provide a simple solution for a very popularly asked question: “how can I download a remote file?.” Well, below is a basic method using the WebClient class. It’s pretty cut and dry, just pass a remote file (ie: http://www.chrisbitting.com/image.jpg) and a local file (c:\temp\file.jpg) and it downloads. If you’re interested how to do this with cookies enabled for authentication, let me know!
vb.net
Private Sub dloadhttpFile(fileToDownload As String, localFile As String)
'Examples:
'fileToDownload = http://www.chrisbitting.com/image.jpg
'localFile = c:\files\someimage.jpg
Try
'create an instance of WebClient - the heart of downloading a file
Dim fileReader As New WebClient()
'check if the file already exists locally
If Not (System.IO.File.Exists(localFile)) Then
fileReader.DownloadFile(fileToDownload, localFile)
End If
Catch ex As HttpListenerException
Console.WriteLine(("Error Downloading: " & fileToDownload & " - ") + ex.Message)
Catch ex As Exception
Console.WriteLine(("Error Downloading: " & fileToDownload & " - ") + ex.Message)
End Try
End Sub
c#
private void dloadhttpFile(string fileToDownload, string localFile)
{
//Examples:
//fileToDownload = http://www.chrisbitting.com/image.jpg
//localFile = c:\files\someimage.jpg
try
{
//create an instance of WebClient - the heart of downloading a file
WebClient fileReader = new WebClient();
//check if the file already exists locally
if (!(System.IO.File.Exists(localFile)))
{
fileReader.DownloadFile(fileToDownload, localFile);
}
}
catch (HttpListenerException ex)
{
Console.WriteLine("Error Downloading: " + fileToDownload + " - " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error Downloading: " + fileToDownload + " - " + ex.Message);
}
}
MVC 4: Code First Migrations with SQL Server – Creating & Updating the DB Schema
Creating and keeping a database schema up to date using asp.Net & MVC 4 in Visual Studio (2010 in the below process) is quite easy and efficient.
Below are some basic steps to get you going.
Step 1. Create a local or remote SQL Server db (I’m using SQL Server 2008 Developer – locally) using SQL Management Studio. Don’t create any tables, etc., just the database.
Step 2. Update the web.config file in your root folder with a connection string for the database you just created. Below is mine:
<connectionStrings> <add name="sampleDBContext" connectionString="Data Source=.\;Initial Catalog=sampleDB;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> </connectionStrings>
It’s important to have the connection string name match the name of the class of your DbSet, below is my dbcontext class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace SampleApp.Models
{
public class sampleDBContext : DbContext
{
public DbSet<simpleTable> simpleTable{ get; set; }
}
}
Step 3. In the Package Manager Console (in Visual Studio), run this command to enable migrations (at the “PM>” prompt):
Enable-Migrations -ContextTypeName SampleApp.Models.sampleDBContext
You should get this result:
Code First Migrations enabled for project SampleApp.
Step 4. At this point, the database hasn’t been updated. To update and create your tables, let’s first create a migration. In the Package Manager Console, run this command to create a migration (at the “PM>” prompt):
Add-Migration InitialMig
(If you were to open the .cs file just created in your migrations folder, you’ll see the create / update commands it will run.)
Step 5. Finally, to actually update the database, run:
Update-Database
(at the “PM>” prompt). You’ll see some messages like:
Applying code-based migrations: [201209122019596_InitialMig]. Applying code-based migration: 201209122019596_InitialMig. Running Seed method.
Now if you look in at your database in SQL Management Studio, you’ll see your changes!
Going forward: go ahead and make changes to your models, you’ll just want to run the “Add-Migration” command, with a new migration name. ie: Add-Migration Mig2 and then run Update-Database to apply the changes. Simple!
Accessing Visual Studio 2010’s built in web server remotely
So as you’re developing / debugging a Visual Studio 2010 web app, you may want to access the local web server from a remote machine. Visual Studio will be accessible locally using an address like: http://localhost:5524, but if you try from another machine on your network using the ip address ie: http://192.168.4.2:5524, you won’t be able to reach the solution.
The easiest way to get this working:
1. Download SPI Port Forward.
2. Install SPI Port Forward.
3. The config is a little backwards in my opinion, so after running SPI Port Forward, enter the remote port you want to use in the “Local Port” text box (ie: 8080), and in the “Remote Host” enter “localhost” (usually) and in the “Remote Port” enter your local Visual Studio debugging port (in my example, 5524.
4. Click Activate
You should now be able to access your Visual Studio web app (provided it’s open and running) using http://(your machine ip)/(the port you choose above) ie: http://192.168.4.2:8080
Happy Testing!
Note: If you have any additional local or network firewalls, they may interfere with the setup and you’ll have additional steps….Net’s System.ServiceModel.Syndication / RSS Feeds!
If you’re looking to work with RSS feeds, I suggest you checkout the .net ServiceModel.Syndication. Below is a quick example in .net 4 / VB:
Imports System.ServiceModel.Syndication
'In case you're using a proxy:
Dim wReq As HttpWebRequest
wReq = WebRequest.Create("http://yourRSSfeedURL")
wReq.Proxy = WebProxy.GetDefaultProxy
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials
Dim xReader As XmlReader = XmlReader.Create(wReq.GetResponse.GetResponseStream)
'Create the feed from the xml loaded
Dim sFeed As SyndicationFeed = SyndicationFeed.Load(xReader)
'Loop through the results
For Each sItem As SyndicationItem In sFeed.Items
Debug.WriteLine(sItem.Title.Text) 'Many other SyndicationItem properties available!
Next
I have compared this to other libraries and this by far is the cleanest / easiest way to pull in RSS Feeds. What are your thoughts?
The Classic: Cannot refer to an instance member of a class from within a shared method…
I am sure you’ve gotten this before:
Cannot refer to an instance member of a class from within a shared method
Well, instead of all the reasons why, I will just tell you one simple solution:
Pretend you’re doing this:
Public Shared Function getSomething() As Integer For Each tControl As Control In FlowLayoutPanel1.Controls Next End FunctionIt will ERROR.
Now Add the form name:
Public Shared Function getSomething() As Integer For Each tControl As Control In sampleForm.FlowLayoutPanel1.Controls Next End FunctionAnd run again!
VB.net Measure string width & height – including multi-line strings
Dim fontToMeasure As New Font("Microsoft Sans Serif", 14, FontStyle.Bold)
Dim sizeOfString As New SizeF
Dim g As Graphics = Me.CreateGraphics
sizeOfString = g.MeasureString("This is line 1" & vbcrlf & "This is line 2", fontToMeasure)
Debug.WriteLine("String Height: " & sizeOfString.Height)
Debug.WriteLine("String Width: " & sizeOfString.Width)
If you want to limit the width (so it wraps), use below (300 was my width to set as max):
Dim fontToMeasure As New Font("Microsoft Sans Serif", 14, FontStyle.Bold)
Dim sizeOfString As New SizeF
Dim g As Graphics = Me.CreateGraphics
sizeOfString = g.MeasureString("This is line 1" & vbcrlf & "This is line 2", fontToMeasure, 300)
Debug.WriteLine("String Height: " & sizeOfString.Height)
Debug.WriteLine("String Width: " & sizeOfString.Width)
Creating & Playing Playlist – VB.Net Windows Media Player Control
'create playlist
Dim newPlayList As WMPLib.IWMPPlaylist = wmpControl.playlistCollection.newPlaylist("soundsToPlay")
newPlayList.appendItem(wmpControl.newMedia("C:\Sample1.mp3"))
newPlayList.appendItem(wmpControl.newMedia("C:\Sample2.mp3"))
'play the list
wmpControl.Visible = False
wmpControl.currentPlaylist = newPlayList
wmpControl.stretchToFit = True
wmpControl.Ctlcontrols.play()
