About jontas

I like to make things that people find useful.

C# parsing through Outlook Calendar items

This code sample will simply parse through all Calendar Items and let us do something with them.
Note: Be aware of what is done here, if there are Items in the Calender with infinite reccurrences (no end date) then this will take forever to parse, so make sure something in the loop causes a break at some point.

	//using the Microsoft Outlook 12 Object library (Microsoft.Office.Interop.Outlook)
	using Microsoft.Office.Interop.Outlook
 
	Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
	Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace= oApp.GetNamespace("MAPI"); ;
	Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder= mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
	Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = CalendarFolder.Items;
 
	outlookCalendarItems.Sort("Start");
	//include recurring events (just a "normal" events i.e once per time it occurs)
	//Have to Sort by Start before setting IncludeRecurrences.
	outlookCalendarItems.IncludeRecurrences = true;
	foreach (Outlook.AppointmentItem item in outlookCalendarItems)
	{
		//Do something with the item
	}

Hiding routevalues when using RedirectToAction – ie don’t show GET params after using RedirectToAction

When we simply try and do a RedirectToAction and simply passes along a model with this then the model is serialized and sent as GET parameters to the next page. This happens because the RedicrectToAction simply returns a HTTP 302 code witch tells the browser to get the next page. If this is not what you want (perhaps you don’t wish to show the user the parameters or allow user modification), or perhaps you wish to pass along some object or similar.

One way around this is to store the data for the next Action in TempData and re-read it in the next action.

public ActionResult Index(MyModel myModel)
{
    TempData["MyModel"] = myModel;
    return RedirectToAction("NextStep", "MyController");
}
 
public ActionResult NextStep(MyModel myModel)
{
    myModel = TempData["MyModel"] as MyModel ?? myModel;
    return View(myModel);
}

system.web.mvc.controller.redirecttoaction at msdn

reverting uncommeted files to the “original” sate (the state that is the last commited)

Sometimes we make attempts that go nowhere or simply edit the wrong file. When this happens it is nice to just “undo” what I did and revert the file back to the last good state (I assume the state I have gotten/pushed to is good).

I can revert the file to the latest version

git checkout -- the.file

Or I can revert it to the latest version from the HEAD revision

git checkout HEAD the.file

Or I could do it for the entire working tree

get reset --hard HEAD

.gitignore all build folders in any subdirectory

Lets say we want to add any and all folders called “build” regardless of where in the directory structure.
Git will by default check each file (and directory), adding limitations will limit when the pattern matches, so actually just typing what you wish to ignore is a global patter.

With git this would be done by adding the following to the .gitignore file

#ignore the folder called build anywhere, but not files called build
build/

Also note that if the directory is already tracked in git, .gitignore will not affect that – it will remain tracked and changes can be committed etc.
Since this has a “/” at the end only directories would be matched – removing it would also make the pattern catch files with the same name.

MVC3 check what url was called (regardless of server/interface)

In some cases there is a need to check if the url that is used to access a page corresponds to what we expect.

This can be done like this

if ( Request.RawUrl.ToString() == Url.Action("LogOut", "Account") ){
    //The url of the action was used to access this page (and no extra GET parameters or anything hinkey) - and any servername/ip we listen to was used
}

This differs from getting the URI in the way that this is the RawUrl is only the part after the server (so for http://example.com/url/to/stuffs RawUrl would contain /url/to/stuffs where the URI would contain details about how the user got there.

system.web.httprequest.rawurl at msdn

Avant Browser don’t show the session restore window

One way to disable the Session restore question (after the browser has crashed/been turned off hard) for Avant Browser is to delete the files that hold the session data.

DEL /Q "%appdata%\Avant Browser\*.opg"
DEL /Q "%appdata%\Avant Browser\reopen.dat"
DEL /Q "%appdata%\Avant Browser\pages.dat"

This can be scripted together with the launch of the browser so that batch(.bat) file could look like:

DEL /Q "%appdata%\Avant Browser\*.opg"
DEL /Q "%appdata%\Avant Browser\reopen.dat"
DEL /Q "%appdata%\Avant Browser\pages.dat"
"C:\Program Files\Avant Browser\IExplore.exe" %1

note: the %1 means to insert the first argument given to the batch file here. So if this is called “avant.bat” and you would call it with “avant.bat www.f15ijp.com” it would open this url.

VirtualBox clone a virtual drive image

Cloning a drive to a new drive is simple:

VBoxManage clonehd existing_current_version.vdi new_target.vdi

Or if you wish to clone to a existing drive use “–existing”

VBoxManage clonehd existing_current_version.vdi existing_target.vdi --existing

Note on using “–existing”: If the existing_target is smaller than the existing_current_version then only the part that will “fit” is copied … so this usually [for windows at least] means that the resulting virtual drive is corrupted; so make sure the target is as large or larger than the existing_version. To modify the size of a virtual drive image check out how to resize a drive.

Note: Older versions of VirtualBox used the command clonevdi instead. At the moment the manual says the following about this “For compatibility with earlier versions of VirtualBox, the “clonevdi” command is also supported and mapped internally to the “clonehd” command.”

VirtualBox resize an existing vdi file

It is possible to change the size of a existing vdi file.

VBoxManage modifyhd --resize newsizeinmb my_vm_drive.vdi

Then use the guest OS to take advantage of this extra space repartition the drives (or simply clone a from a larger drive if that is suitable).

Opera 8.54 don’t show the session restore window

One way to disable the Session restore question (after the browser has crashed/been turned off hard) on Opera 8.54 is to delete the files that hold the session data.

DEL /Q "C:\Program Files\Opera\profile\sessions\autosave.win"
DEL /Q "C:\Program Files\Opera\profile\sessions\autosave.win.bak"

This can be scripted together with the launch of the browser so that batch(.bat) file could look like:

DEL /Q "C:\Program Files\Opera\profile\sessions\autosave.win"
DEL /Q "C:\Program Files\Opera\profile\sessions\autosave.win.bak"
"c:\Program Files\opera\opera.exe" %1

note: the %1 means to insert the first argument given to the batch file here. So if this is called “opera.bat” and you would call it with “opera.bat www.f15ijp.com” it would open this url.