Monday, June 29, 2009

Create Windows startup shortcut for ClickOnce application

ClickOnce does not provide an option for adding the application to start automatically when starting Windows. You can use the concepts in the following method to add or remove this option. You will need to make sure “Allow URL parameters to be passed to the application” is checked. This if found within Project Properties –> Publish tab –> Options –> Manifests.

using System;
using System.Deployment.Application; //Requires System.Deployment assembly
using System.IO;
using System.Text;
using System.Windows.Forms;
...
private void CreateWindowsStartup(bool create)
{
string startupFileName = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".appref-ms";
string applicationShortcut = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
if (create)
{
if (!File.Exists(startupFileName))
{
using (FileStream fs = new FileStream(startupFileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode))
{
sw.WriteLine(applicationShortcut);
}
}
}
}
else
{
if (File.Exists(startupFileName))
{
File.Delete(startupFileName);
}
}
}



 




Friday, June 26, 2009

C# SMTP Snippet

I recently provided this simple snippet to a colleague and thought I’d share it out. It includes a way to attach a local file or attach one generated on the fly.
using System;
using System.IO;
using System.Net.Mail;
using System.Text;
...
public static void SendEmail()
{
try
{
bool createFile = true;
string fileName = @"c:\boot.ini";
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.From = new MailAddress("you@domain.com");
mail.To.Add(new MailAddress("you@domain.com"));
mail.Subject = "Subject";
mail.Body = "Body";
if (createFile)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes("File Content Blah Blah Blah");
MemoryStream ms = new MemoryStream(bytes);
mail.Attachments.Add(new Attachment(ms, "FileTitle" + ".txt", "text/plain"));
}
else
{
mail.Attachments.Add(new Attachment(fileName));
}
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "SMTPSERVERNAME";
smtpClient.Send(mail);
}
catch (Exception ex)
{

}
}

Tuesday, June 23, 2009

IIS Basics: Create a URL Redirect

Sometimes you may end up very long URLs like http://internal/sites/hr/benefits/default.aspx.  You can easily create a redirect which allows users to have a URL like http://benefits by using the following steps:

1. Create a DNS entry that contains the name you want used and the server you want it mapped to. You may need to request this from your network team. See the following for more details: Add an alias (CNAME) resource record to a zone

2. Open Start --> Run and type: inetmgr

clip_image002

3. Click OK.

4. Open the tree view navigation on the left pane until you find Web Sites.

5. Right-click on Web Sites and click on New –> Web Site…

clip_image004

6. Click Next when the dialog opens.

clip_image006

7. Type in a description and click Next.

clip_image008

8. Enter the host header and click Next.

clip_image010

9. For the time being, enter c:\ and click Next. This gets changed later on when this is changed to a redirect.

clip_image012

10. Click Next.

clip_image014

11. Click Finish.

clip_image016

12. Right-click on the name you used in step 7 and click Properties.

clip_image018

13. Click on the Home Directory tab, change the top radio button to A redirection to a URL.

14. Enter the URL into the Redirect to textbox and click the The exact URL entered above checkbox.

clip_image020

15. Click OK.

16. Open a browser and type http://yourredirectname to test.

Friday, June 5, 2009

Deep Zoom Batch Processing using DeepZoomTools.dll

The following code will open a folder browser dialog and allow you to recursively batch process multiple images using the DeepZoomTools.dll that is included when you download Deep Zoom Composer. I also added additional methods for creating a CSV of your directory structure so you can filter out your dzi files and export into a data source.

Note: This is part of the April 2009 version of Deep Zoom Composer.

Steps:

1. Create a Window Form Application in Visual Studio.

2. Make a reference to the DeepZoomTools.dll found in C:\Program Files\Microsoft Expression\Deep Zoom Composer.

3. Add the following code and call the GenerateDeepZoomImages method.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.DeepZoomTools;
public void GenerateDeepZoomImages()
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
DialogResult result = folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
List<string> extensionFilter = new List<string> { ".tiff", ".tif", ".jpg", ".bmp", ".png" }; //DeepZoom (April 2009) only supports these extensions
List<FileSystemInfo> fileList = GetFileSystemInfo(folderBrowser.SelectedPath, false, true, true, extensionFilter);
CollectionCreator collectionCreator = new CollectionCreator();
ImageCreator imageCreator = new ImageCreator();
List<Microsoft.DeepZoomTools.Image> images = new List<Microsoft.DeepZoomTools.Image>();
foreach (FileInfo fsInfo in fileList)
{
string imageFileName = fsInfo.FullName;
string imageFolderName = Path.GetDirectoryName(fsInfo.FullName);
string destinationFolder = imageFolderName + "\\" + fsInfo.Name + "_dzdata";
imageCreator.Create(fsInfo.FullName, destinationFolder);
Microsoft.DeepZoomTools.Image img = new Microsoft.DeepZoomTools.Image(imageFileName);
//You can manipulate this Image object before adding
images.Add(img);
collectionCreator.Create(images, destinationFolder);
}
}
}

public List<FileSystemInfo> GetFileSystemInfo(string directoryPath, bool includeFolders, bool includeFiles, bool recursive, List<string> extensionFilter)
{
extensionFilter = (from e in extensionFilter select e.ToLower()).ToList<string>();
List<FileSystemInfo> fileSystemList = new List<FileSystemInfo>();
DirectoryInfo directory = new DirectoryInfo(directoryPath);
if (recursive)
{
foreach (DirectoryInfo childDirectory in directory.GetDirectories())
{
fileSystemList.AddRange(GetFileSystemInfo(childDirectory.FullName, includeFolders, includeFiles, recursive, extensionFilter));
}
}
if (includeFolders)
{
fileSystemList.Add(directory);
}
FileInfo[] files = directory.GetFiles("*.*");
foreach (FileInfo file in files)
{
if (extensionFilter.Count != 0)
{
if (extensionFilter.Contains(file.Extension.ToLower()))
{
fileSystemList.Add(file);
}
}
else
{
fileSystemList.Add(file);
}
}
return fileSystemList;
}
public static string GetFileSystemCsv(List<FileSystemInfo> fileSystemList)
{
StringBuilder sb = new StringBuilder();
sb.Append("Name,Full Name,Parent Folder,Creation Time,Last Access Time,Last Write Time,Length, Extension,Attributes\r\n");
foreach (FileSystemInfo fileSystemItem in fileSystemList)
{
if (fileSystemItem.GetType() == typeof(FileInfo))
{
sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}\r\n", fileSystemItem.Name, fileSystemItem.FullName, fileSystemItem.FullName.Replace(fileSystemItem.Name, ""), fileSystemItem.CreationTime, fileSystemItem.LastAccessTime, fileSystemItem.LastWriteTime, ((FileInfo)fileSystemItem).Length.ToString(), fileSystemItem.Extension, fileSystemItem.Attributes.ToString()));
}
else
{
sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}\r\n", fileSystemItem.Name, fileSystemItem.FullName, fileSystemItem.FullName.Replace(fileSystemItem.Name, ""), fileSystemItem.CreationTime, fileSystemItem.LastAccessTime, fileSystemItem.LastWriteTime, "", "", fileSystemItem.Attributes.ToString()));
}
}
return sb.ToString();
}

private void SaveCsvFile(string fileText)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "CSV Files (*.csv)*.csv";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write(fileText);
wText.Close();
}
}
}