Wednesday, April 27, 2016 | By: nika perales

Opening notepad from C# app

In this post i'll show you how to open notepad windows application from our C# project. This is useful when you want to display a particular text file to a user.
Private void RunNotepad()
{
 Process Notepadprocess  = new Process();
 Notepadprocess.StartInfo = new ProcessStartInfo("notepad.exe", "E:\\New.txt");
 Notepadprocess.Start();
}
 

C# random filename generator

Import namespace
using System.IO;
Code
string path = @"C:\Folder\file.txt";

Console.WriteLine(path);
Console.WriteLine();

Console.WriteLine("Directory name: {0}", Path.GetDirectoryName(path));
Console.WriteLine("File name: {0}", Path.GetFileName(path));
Console.WriteLine("File name without extension: {0}", Path.GetFileNameWithoutExtension(path));
Console.WriteLine("File extension: {0}", Path.GetExtension(path));

Sample output

Directory name: C:\Folder
File name: file.txt
File name without extension: file
File extension: .txt

Read available disk space

Does your program need to get the available disk space on your computer. Here's the .net code
Import namespace
using System.IO;

Code
using System.IO;
DriveInfo drive = new DriveInfo(@"C:\");
long space = drive.AvailableFreeSpace;
Tuesday, September 6, 2011 | By: nika perales

OLEDB Error "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."

I was recently working on a software system that uses either msaccess or mysql for its backend. Everything worked well while i was using mysql for the backend but suddenly out of nowhere this error "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)" pops-up out of nowhere, i have traced and re-traced my code and it turned out that the culprit was a query that used a mysql built in function for strings. After getting rid of the mysql function everything ran smoothly. I'm posting this in the hopes that it may help someone who encounters similar error...
Friday, August 12, 2011 | By: nika perales

VB.net: connect to ms access database

Here's how to connect to an MS Access Database from VB.Net and add a new record. The sample code uses the OleDbConnection object as a means of connecting to the MS Access Database...

Imports System.Data

Public Class UsingMsAccess


Private Sub AddNewRecord()
'Replace path to "accessdatabasepath" with the actual path to the ms access database
	'example C:\testApp\data\mydata.mdb
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=accessdatabasepath"
        Dim dbConnection As DbConnection = New OleDbConnection(connectionString )
	
        Dim queryStr As String = "SELECT [Employees].* FROM [Employees]"
        Dim dbCommand As DbCommand = New OleDbCommand
        
        dbConnection.Open()
	dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dataAdapter As DbDataAdapter = New OleDbDataAdapter
        Dim commandBuilder as DbCommandBuilder= New OleDbCommandBuilder(dataAdapter)
	dataAdapter.SelectCommand = dbCommand
        Dim MydataSet As DataSet = New DataSet
        dataAdapter.Fill(MydataSet,"Employees") 'where employees is the name of the datatable
        
	dim NewRow as datarow = MydataSet.tables("Employees").Newrow
	NewRow("Name") = "sammy" 'Replace ("Name") with your own table field name
	NewRow("Age") = 27
	
	MydataSet.tables("Employees").Rows.Add(NewRow)
	dataAdapter.Update(MydataSet,"Employees")
	MydataSet.tables("Employees").AcceptChanges()

End Sub