Posts tagged with "VB.Net"

Adding Metadata Progmattically

Posted within Development on by Ryan Ball

Dim meta As HtmlMeta = New HtmlMeta()
meta.HttpEquiv = "refresh"
meta.Content = "600"
Page.Header.Controls.Add(meta)

Detecting Mobile Devices with ASP.NET

Posted within Development on by Ryan Ball

I’ve no imediate use for this code, but I know wthat it will be useful in the future – especially as the number of user’s accessing websites via mobile phones and PDAs grow.

Taken from a post by truelove.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim Browser_obj As System.Web.Mobile.MobileCapabilities = CType(Request.Browser, System.Web.Mobile.MobileCapabilities)
If Browser_obj.Browser = "Pocket IE" Then
Label1.Text = "the is Pocket PC"
ElseIf Browser_obj.Browser = "IE" Then
Label1.Text = "Microsoft Internet Explorer"
ElseIf Browser_obj.Browser = "Phone.com" Then
Label1.Text = "the is Openwave"
End If
End If
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim browser As System.Web.Mobile.MobileCapabilities = CType(Request.Browser, System.Web.Mobile.MobileCapabilities)
If browser.ScreenCharactersWidth < 20 Then
Label1.Text = "short text message"
Else
Label1.Text = "long text message"
End If
End If
End Sub

Strip HTML Tags from a String

Posted within Development on by Ryan Ball

Private Function ConvertHtmlToPlainText(ByVal htmlText As String) As String
Return Regex.Replace(htmlText, "<[^>]*>", String.Empty)
End Function

Sending Plain Emails

Posted within Development on by Ryan Ball

Yet another of my “I always use it but always forget it” posts, this time detailsing how to correctly send emails via SMTP in VB.Net.

'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is a sample body"

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)

Additional information can be found at System.Net.Mail.

Appending Items to a DropDownList

Posted within Development on by Ryan Ball

So that I don’t forget this in the future… to append additional items to a DropDownList control, use the following code.

Dim itemPosition as Integer = 0
MyDropDownList.Items.Insert(itemPosition, New ListItem("TextField", "ValueField"))MyDropDownList.SelectedIndex = 0

Formatting Dates within ASP.Net

Posted within Development on by Ryan Ball

As I am always formatting dates and always forgetting the parameters to do so, I am placing them here as a quick reference.

Dim timeNow as DateTime = DateTime.Now
Dim timeFormatted as String = rightNow.ToString("MMM dd, yyyy")

Dim timeNow as DateTime = DateTime.Now
Dim timeFormatted as String = String.Format("{0:MMM dd, yyyy}", timeNow)

Dim timeNow As New DateTime(2000, 1, 1, 0, 0, 0)
Dim timeFormatted As String = timeNow.ToString("dddd - d - MMMM")
' In the U.S. English culture, timeFormatted has the value:
' "Saturday - 1 - January".
timeFormatted = timeNow.ToString("yyyy gg")
' In the U.S. English culture, timeFormatted has the value: "2000 A.D.".

Pattern Description
d ShortDatePattern
D LongDatePattern
f Full date and time (long date and short time)
F FullDateTimePattern (long date and long time)
g General (short date and short time)
G General (short date and long time)
m, M MonthDayPattern
r, R RFC1123Pattern
s SortableDateTimePattern (based on ISO 8601) using local time
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern using the format for universal time display
U Full date and time (long date and long time) using universal time
y, Y YearMonthPattern

The table below shows the case-sensative patterns that are available to construct custom patterns.   

Pattern Description
d The day of the month. Single-digit days will not have a leading zero.
dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week, as defined in AbbreviatedDayNames.
dddd The full name of the day of the week, as defined in DayNames.
M The numeric month. Single-digit months will not have a leading zero.
MM The numeric month. Single-digit months will have a leading zero.
MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames.
MMMM The full name of the month, as defined in MonthNames.
y The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
yy The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyyy The year in four digits, including the century.
gg The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
h The hour in a 12-hour clock. Single-digit hours will not have a leading zero.
hh The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H The hour in a 24-hour clock. Single-digit hours will not have a leading zero.
HH The hour in a 24-hour clock. Single-digit hours will have a leading zero.
m The minute. Single-digit minutes will not have a leading zero.
mm The minute. Single-digit minutes will have a leading zero.
s The second. Single-digit seconds will not have a leading zero.
ss The second. Single-digit seconds will have a leading zero.
f The fraction of a second in single-digit precision. The remaining digits are truncated.
ff The fraction of a second in double-digit precision. The remaining digits are truncated.
fff The fraction of a second in three-digit precision. The remaining digits are truncated.
ffff The fraction of a second in four-digit precision. The remaining digits are truncated.
fffff The fraction of a second in five-digit precision. The remaining digits are truncated.
ffffff The fraction of a second in six-digit precision. The remaining digits are truncated.
fffffff The fraction of a second in seven-digit precision. The remaining digits are truncated.
t The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any.
tt The AM/PM designator defined in AMDesignator or PMDesignator, if any.
z The time zone offset (“+” or “-” followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is “-8″.
zz The time zone offset (“+” or “-” followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is “-08″.
zzz The full time zone offset (“+” or “-” followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is “-08:00″.

For additional information see:

Windows Media

Posted within Development on by Ryan Ball

Part of today was spent researching Windows Media Video (WMV) files and how I could interact with them in VB.Net. As with quite a few areas of .Net, this was one where the documentation was very difficult to track down.

I began my “research” by including FFmpeg within my project folder. By executing this via the code shown below I was able to loop through the 200+ video files hosted upon our streaming server and generate a thumbnail for each one. I did look at using SharpFFmpeg, a C# binding of FFmpeg, but its lack of documentation quickly made me look elsewhere.

Dim ExecutablePath As String = "C:\Web Server\Components\ffmpeg.exe"
Dim myProcess As New Diagnostics.Process()
Dim myProcessStartInfo As New Diagnostics.ProcessStartInfo(ExecutablePath)
myProcessStartInfo.Arguments = String.Format(" -an -y -t 0:0:0.001 -ss 00:00:5 -i ""{0}"" -f mjpeg -s 120x90 ""{1}""", FilePath, Thumbnail)
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardInput = True
myProcessStartInfo.RedirectStandardOutput = True
myProcessStartInfo.RedirectStandardError = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()
Dim myStreamReader As StreamReader = myProcess.StandardOutput
Dim myString As String = myStreamReader.ReadToEnd()

myProcess.WaitForExit(30000)
myProcess.Close()