by aherrick
29. November 2009 07:13
I wrote this little Windows Forms application a while back as an answer to a small issue I was having with my external hard drives. I wanted to prevent them from spinning down and entering a sleep mode after a certain period of time. This application simply attempts to write a text file to the root of each configured drive in order to keep activity on the drive and prevent it from spinning down. I wanted to learn how to do some of the different configurations but mainly wanted to write a working Windows application. Tested in Windows Server 2003/2008, Windows XP, and Windows Vista.

It is fully customizable to allow for multiple hard drives, and has different configuration settings such as how often (in minutes) to write a file, minimize to tray on start, monitor automatically on start, and run on Windows start up. Once a configuration is saved, settings are saved even if the application is restarted. I wanted to take a look at some of these basic configurations that can be reused across other applications that I build.

Below is an EXE of the application and the full source in case you are interested in checking it out!
HardDriveNoSleepV1.0-EXE.zip (91.46 kb)
HardDriveNoSleepV1.0-SRC.zip (422.66 kb)
by aherrick
9. December 2008 11:11
I am releasing this application I created to help me organize my music collection. It takes multiple paths and combines ever directory under each path into a single txt/csv file sorted alphabetically. I had a little fun with some of the validation and colors to make it work a little more smoothly.
Usage:
- Select paths using Add
- Select output format (txt or csv)
- List is generated to same directory .exe is located
Attached is the .exe and full source code. It is free for distribution and please feel free to tweak to your needs!
If I tweak this in the future I will post the updated code, consider this V1.0
Enjoy!
DirectoryListV1.0-FullSRC.zip (73.37 kb)
DirectoryListV1.0EXE.zip (12.18 kb)
by aherrick
3. December 2008 06:34
I was working on a project and was looking for more control over the elements I was creating in the HTML. More specifically, I had a MasterPage setup and I wanted all pages to have the same background image throughout the site.
Simple. Add a runat=server to your markup. See below.
<div id="bgimg" runat="server">
</div>
You now have control of this element from the code-behind.
To add a background image to the div, you could do something like this:
bgimg.Style[HtmlTextWriterStyle.BackgroundImage] = ResolveUrl("~/images/bodyback.jpg");
The only thing to realize is what your generated markup is. Asp.Net will auto-generate this tag to prevent naming collisions. Just look out for it if you are trying to style the element with CSS or call it using javascript.
<div id="ctl00_bgimg" style="background-image:url(/HRR/images/bodyback.jpg);">
</div>
Enjoy!
by aherrick
29. September 2008 15:06
I was looking for an easy solution to add Twitter to my blog, and decide to write a class extension to fit my needs. With input from Fence Row Productions I came up with a class to easily grab your most recent tweet and display it on your blog. The code will parse Twitter API to display your most recent Tweet.
Check out how I am currently using it.
Added two ASP.NET label controls "lblTweet" and "lblTweetInfo." Also added an image control "imgTweet."
Here is my current code behind file for my MasterPage. The class constructor is your Twitter login name (twitter.com/you)
imgTweet.ImageUrl = "~/pics/twitter_bird_32.png";
Twitter t = new Twitter("andrew_herrick");
this.lblTweet.Text = t.TweetStatus;
this.lblTweetInfo.Text =
string.Format("<a href='{0}' target='NEW'>{1}</a> from {2}.",
t.TweetURL, t.TweetDateDiff, t.TweetSource);
Attached is the complete class. Let me know how it goes and enjoy!
Twitter.cs (4.65 kb)
1035ce83-c466-4359-889d-f7db1278f146|3|4.3
Tags: c#
by aherrick
5. September 2008 04:21
I was working on my Twitter extension to display my most recent tweet and I wanted to display how long ago I tweeted. I was able to acquire some code which I tweaked a bit to fit my needs. Check it out!
This method accepts two dates that you want to compare. For example: In my case, date1 is "DateTime.Now" and date2 is the Tweet date (whenever my most recent tweet was). I will have a full post on the twitter extension you see above soon!
Take a look at the code below!
private string GetDifferenceDate(DateTime date1, DateTime date2)
{
if (DateTime.Compare(date1, date2) >= 0)
{
TimeSpan ts = date1.Subtract(date2);
// if more than 1 day ago
if (ts.Days > 0)
return string.Format("{0} days, {1} hours, {2} minutes",
ts.Days, ts.Hours, ts.Minutes);
else
return string.Format("{0} hours, {1} minutes", ts.Hours, ts.Minutes);
}
else
return "Not valid";
}
3cf90c33-742a-49a9-9367-4ae46a9d97bc|1|4.0
Tags: c#
by aherrick
12. August 2008 23:55
I was working on form submit where I wanted to prompt with a simple Javascript confirm dialog when the user clicks the submit button on the form. Naturally, all the form fields (name, adress, etc.) I setup using required field validators. My custom javascript confirm originally looked like this.
I called the javascript from the code behind.
The big problem I found was that the javascript confirm function was being called regardless of if the page completely validated or not. I wanted the functionality to first validate the page, then if the page was valid, run the custom code. I came up with this code using Page_IsValid. It uses Page_ClientValidate() to validate the page, then I check if the page is valid. Simple yet effective!
Good luck!
by aherrick
21. July 2008 00:54
I have been working with SubSonic for almost a year now and I love it. If you don't know, SubSonic is a data access layer that will automatically generate code to access your data along with a bunch of other really cool tools. Created by Rob Conery, check him out if you have not, version SubSonic 2.1 was just released. I'm going to try and blog about different things I pick up about SubSonic that I hope proves to be useful.
Creating a partial class to extend a a generated SubSonic class is easy! Let's take a look...
When creating SubSonic based projects, we like to create a class library to abstract SubSonic from the web project. You can name this project whatever you like.
In this example I am going to use the Products table from Microsoft's Northwind database. In my SubSonic project class I create a new folder to hold my partial class. I call the folder "DAL"
Create a new class in your DAL folder with the same name as the SubSonic class. For example, SubSonic generates "Product.cs". If you wanted to extend this class, the only trick is to put the keyword "partial" in your new class. See below.
I am going to write a simple method to extend the generated SubSonic Product class. This method "GetProductsToOrder" will return a collection of products where UnitsInStock is less than 20. Of course this is just an arbitrary business rule used as an example. See the code below.
Now to use this method, since I made it static, I do not have to create a new instance of Product. I will set it equal to a ProductCollection and pass it an integer.
Now I have a collection of Products with less than twenty units in stock! Good luck writing your SubSonic partial classes and let me know how it goes!
by aherrick
9. July 2008 05:12
Today I was working on some code and I needed to validate a textbox input when the user attempted to leave the field. The javascript event onblur fit the bill nicely.
In the code behind I wired up my textbox like so:
this.txtAmount.attributes.add("onblur", "validate()");
This will hook the onblur event onto the textbox programatically. I find it appropriate to add events progrmatically since Visual Studio complains about adding events to the markup. While this will still compile and run just fine, there is much more flexiblity in adding events in the code behind.
Now for the javascript code. First I am setting a reference to my textbox and a hidden field on the form. The first check is a regular express to make sure my input is valid. I am looking for positive amount.
For example: 2 valid, 200 valid, 200.20 valid, 200.233 invalid, -200 invalid.
If input does not pass the regex, I throw an alert asking for valid input. If it passes regex, my second check is just some business logic.
That is all it takes to write a custom javascript validation for your code. Thank you!
by aherrick
8. July 2008 07:50
Today I was working on some code and I needed to programatically create a link with the ability to have a javascript pop up. The code is as follows:
string javascriptLink = "<a href=\"javascript: void(0)\" onclick =\"window.open('YourPageHere.aspx', '', 'scrollbars=0');return false;\">javascript link</a>";
To take this code a little further as in my case I needed to pass a query string to the page I wanted to load.
Edit your string like so:
string javascriptLink = "<a href=\"javascript: void(0)\" onclick =\"window.open('YourPageHere.aspx?Id=" + yourId + "', '', 'scrollbars=0');return false;\">javascript link</a>";
where yourId is the variable you want to pass.
Let me know if it worked for you. It's just that easy!
9eca0a68-d54a-4f08-9d4a-8ce3f47185ea|0|.0
Tags: javascript, c#