This blog is a depository for things I have found regarding VB.NET and C#.NET development (and other things I may need to remember). This blog is primarily for my own reference, but if the information I found is useful to others, then that is great. If others want to contribute with comments or other information, then that is great too.
Friday, July 27, 2007
Formatting Number, Currency, Dates, and Time in VB.NET
http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-08.aspx
Friday, July 20, 2007
Path and Filename Decomposition function in VB.NET and C#
filename = Path.GetFileName(Path)
extension = Path.GetExtension(Path)
fullPath = Path.GetFullPath(Path)
directoryName = Path.GetDirectoryName(Path)
FilenameWithoutExt = Path.GetFileNameWithoutExtension(Path)
However, I have need to use these function for similar things that the Path class cannot handle, so I am including them in this posting.
Here are 4 functions in VB.NET and C# that do path and filename decomposition:
1) GetFileNameFromPath will return a filename and its extension from a full directory path.
GetFileNameFromPath("c:\temp\test.txt") returns "test.txt"
GetFileNameFromPath("test.txt") returns "test.txt"
2) GetFileNameWithoutExtFromPath will return a filename without its extension from a full directory path.
GetFileNameWithoutExtFromPath("c:\temp\test.txt") returns "test"
GetFileNameWithoutExtFromPath("test.txt") returns "test"
GetDirFromPath("c:\temp\test.txt") returns "C:\temp"
GetDirFromPath("test.txt") returns ""
4) GetExtensionFromPath will return only the file extension from either a filename or a full path.
GetExtensionFromPath("c:\temp\test.txt") returns ".txt"
GetExtensionFromPath("test.txt") returns ".txt"
VB.NET Code:
Public Function GetDirFromPath(ByVal path As String) _
As String
Try
Return path.Substring(0, path.LastIndexOf("\") + 1)
Catch ex As Exception
' error
Return ""
End Try
End Function
'
Public Function GetFileNameFromPath(ByVal path _
As String) As String
Try
Return path.Substring(path.LastIndexOf("\") + 1)
Catch ex As Exception
' error
Return ""
End Try
End Function
'
Public Function GetFileNameWithoutExtFromPath(ByVal path _
As String) As String
Try
Dim filename As String = _
path.Substring(path.LastIndexOf("\") + 1)
Dim pos As Integer = filename.LastIndexOf(".")
If pos <> -1 Then
Return filename.Substring(0, pos)
Else
Return filename
End If
Catch ex As Exception
' error
Return ""
End Try
End Function
'
Public Function GetExtensionFromPath(ByVal path _
As String) As String
Try
Dim pos As Integer = _
path.LastIndexOf(".")
If pos <> -1 Then
Return path.Substring(pos)
Else
Return ""
End If
Catch ex As Exception
' error
Return ""
End Try
End Function
C# Code:
public String GetDirFromPath(String path)
{
try
{
return path.Substring(0, path.LastIndexOf("\\") + 1);
}
catch (Exception ex)
{
// error
return "";
}
}
public String GetFileNameFromPath(String path)
{
try
{
return path.Substring(
path.LastIndexOf("\\") + 1);
}
catch (Exception ex)
{
// error
return "";
}
}
public String GetFileNameWithoutExtFromPath(String path)
{
try
{
String filename = path.Substring(
path.LastIndexOf("\\") + 1);
int pos = filename.LastIndexOf(".");
if (pos != -1)
return filename.Substring(0, pos);
else
return filename;
}
catch (Exception ex)
{
// error
return "";
}
}
public String GetExtensionFromPath(String path)
{
try
{
int pos = path.LastIndexOf(".");
if (pos != -1)
return path.Substring(pos);
else
return "";
}
catch (Exception ex)
{
// error
return "";
}
}
Thursday, July 19, 2007
Sharing a file between Multiple Projects in VB.NET or C#
Generating a GUID in Windows Mobile using VB.NET or C#
http://msdn2.microsoft.com/en-us/library/aa446557.aspx
Finding the My Documents Directory in Windows Mobile (or the Desktop)
path = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal)
GetFolderPath also works on the desktop.
For More Info:
http://msdn2.microsoft.com/en-us/library/system.environment.getfolderpath.aspx
http://blogs.msdn.com/windowsmobile/archive/2006/07/26/678742.aspx
Camera API for Windows Mobile
http://blogs.msdn.com/vsdteam/archive/2005/06/29/433961.aspx
http://msdn2.microsoft.com/en-us/library/microsoft.windowsmobile.forms.cameracapturedialog_members.aspx
http://www.google.com/search?q=camerapresent&sourceid=ie7&rls=com.microsoft:en-US&ie=utf8&oe=utf8
http://channel9.msdn.com/ShowPost.aspx?PostID=209787
Wednesday, July 18, 2007
File Handles are automatically closed for Files on a Storage Card when a Windows Mobile Device goes to Sleep
Here is some more information as to why:
http://blogs.msdn.com/windowsmobile/archive/2007/01/12/everything-you-want-to-know-about-sd.aspx
SetLength in VC++ will Throw Exception in Windows Mobile 6 emulator when file is in Shared Directory
If you have a Windows Mobile 6 Emulator running (and appearently this error is also true for a few versions back), and you are sharing a directory between the desktop file system and the emulator (Shared Folder under the Emulator's Configure dialog will make a desktop directory show up as a \Storage Card directory on the emulator), then if you try to use CFile::SetLength on a file in the shared directory, the application will throw an exception for no apparent reason. I am also found other saying that FlushFileBuffers also has the same problem.
Here is the only other reference I have found to this problem:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=796830&SiteID=1
Friday, July 13, 2007
Optional Parameters and parameters with default values in VB.NET
Public Function SplitAttrValue(ByVal str As String, ByRef attr As String, ByRef value As String, Optional ByVal delimiter As String = "=") As Boolean
However, here is an article that describes why you should not do this:
http://www.knowdotnet.com/articles/optionalparams.html
Main reason is that C# doesn't support them (you must use overloading).
Using Reflection in VB.NET
http://www.java2s.com/Code/VB/Development/UseReflectiontocreateClassinstanceandcallmethod.htm
Tuesday, July 10, 2007
Passing a Function by Reference in VB.NET
The key to making this work is to define a delegate with the right parameter signature, then use the delegate's name (TestDelegate in my example) when defining the type for the function you are passing.
Example:
Public Class TestResult
Public name As String = ""
Public pass As Boolean = False
Public startTime As DateTime
Public endTime As DateTime
Public comments As String = ""
End Class
Public Delegate Sub TestDelegate(ByVal result As TestResult)
Private Sub RunTest(ByVal testFunction As TestDelegate)
Dim result As New TestResult
result.startTime = DateTime.Now
testFunction(result)
result.endTime = DateTime.Now
End Sub
Private Sub MenuItemStartTests_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MenuItemStartTests.Click
Debug.WriteLine("Starting Tests...")
Debug.WriteLine("")
'==================================
' Add Calls to Test Modules Here
RunTest(AddressOf Test1)
RunTest(AddressOf Test2)
'==================================
Debug.WriteLine("")
Debug.WriteLine("Tests Completed")
End Sub
Private Sub Test1(ByVal result As TestResult)
result.name = "Test #1"
.
.
.
result.pass = True
End Sub
Private Sub Test2(ByVal result As TestResult)
result.name = "Test #2"
.
.
.
result.pass = True
End Sub
Thursday, July 5, 2007
Gridlines in a Listview with .NET Compact Framework
Look at the Grid in Listview (C# and VB.NET) item on this page:
http://www.businessanyplace.net/?p=code#listviewgrid