JQuery UI Autocomplete

Following code allows you to change the behaviour of the JQuery Autocomplete plugin to allow matching items to be clicked and following a specific URL.

<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  <style type="text/css">
  
  .ui-menu .ui-menu-item a
  {
  text-decoration:underline;
  cursor:pointer;
  cursor:hand;
  }
  
  </style>

  
  <script>
  $(document).ready(function() {
  
    $("input#autocomplete").autocomplete({
    source: [ { id : 1, value : "c++" }, 
              { id : 2, value : "java" }, 
              { id : 3, value : "php" }, 
              { id : 4, value : "coldfusion" }, 
              { id : 5, value : "javascript" }, 
              { id : 6, value : "asp" }, 
              { id : 7, value : "ruby" }
            ],
    open: function(event, ui) { 
                                  $("ul.ui-autocomplete").unbind("click");
                                
                                  var data = $(this).data("autocomplete");
                                  console.log(data);
                                  
                                  for(var i=0; i<=data.options.source.length-1;i++)
                                  {
                                    var s = data.options.source[i];
                                    $("li.ui-menu-item a:contains(" + s.value + ")").attr("href", "directory/listing/" + s.id);
                                  }
                                  
                                } 

    });
        
    /*
    $("input#autocomplete").bind("autocompleteselect", function(event, ui) { 
        //alert(ui.item.id + ' - ' +  ui.item.value); 
        //document.location.href = ui.item.id + '/' + ui.item.value;
        //event.preventDefault; 
        } );
    */
    
  });
  </script>
</head>
<body style="font-size:62.5%;">
  
<input id="autocomplete" />

</body>
</html>

Search in MS Word .docx files

I needed to check a load of MS Word (.docx) documents to make sure that a change had been made correctly so I decided to create a utility that would go through all the directories checking the docs and listing any that were incorrect.

As everybody knows the new .docx files are really .zip files containing all the files required to make the final document so you can rename the .docx file to .zip and then open it using Winzip, 7-ZIP or whatever your preferred unzipping tool is.

The file I was interested in is under the \word folder called document.xml. This contains the document text and can be extracted to be edited.

To do the unzipping from within C# I made use of a .net library called DotNetZip from here. Once I have the document I then run a regex on it to match any field code that I was interested in and then run another regex on the field codes to make sure they are valid.

Here’s the bulk of the code that does all the work….

private static void ProcessFiles(string directoryName, string filename, bool includeSubDirectories)
{

   Console.WriteLine(directoryName);
   Console.WriteLine();

   foreach (string f in System.IO.Directory.EnumerateFiles(directoryName, filename))
   {

       Console.WriteLine(System.IO.Path.GetFileName(f));

       using (ZipFile zip1 = ZipFile.Read(f))
       {

           // here, we extract every entry, but we could extract conditionally
           // based on entry name, size, date, checkbox status, etc. 
           foreach (ZipEntry e in zip1)
           {
               if (e.FileName.Contains("document.xml") && !e.FileName.Contains("document.xml.rels"))
               {

                   var fieldErrors = false;

                   // Grab the document.xml file
                   e.Extract(System.IO.Path.GetTempPath(), ExtractExistingFileAction.OverwriteSilently);

                   // Open up file to scan for field codes
                   var fileContents = System.IO.File.ReadAllText(System.IO.Path.GetTempPath() + e.FileName);

                   // Now regex it to find the field codes
                   Regex regex = new Regex(
                     "(?<begin>{)(?<content>.*?)(?<end>})\r\n",
                   RegexOptions.IgnoreCase
                   | RegexOptions.CultureInvariant
                   | RegexOptions.IgnorePatternWhitespace
                   | RegexOptions.Compiled
                   );

                   MatchCollection ms = regex.Matches(fileContents);

                   List<string> fields = new List<string>();
                   // and then double check they are valid field codes (no < / > chars)
                   foreach (var m in ms)
                   {

                       regex = new Regex(
                         "<.*?>",
                       RegexOptions.IgnoreCase
                       | RegexOptions.CultureInvariant
                       | RegexOptions.IgnorePatternWhitespace
                       | RegexOptions.Compiled
                       );

                       string result = m.ToString();
                       if (regex.IsMatch(m.ToString()))
                       {
                           result = regex.Replace(m.ToString(), string.Empty) + " *";
                       }

                       Console.WriteLine("\t" + result);

                   }

                   Console.WriteLine();
               }
           }
       }

   }

   if (includeSubDirectories)
   {
       foreach (var d in System.IO.Directory.GetDirectories(directoryName))
       {
           ProcessFiles(d, filename, true);
       }
   }

}

Special day in history…

The future is now...  today is the day that Marty McFly arrived in the future after hitting 88mph in a pimped out Delorean in 1985. 

clip_image001

clip_image001[4]

Vault Diff not working in VS2010

Found this regarding the problem and it solved it for me.

(Need to go to Tools / Options then select Source Control, Plug-In settings and Advanced)

post_screenshot

Get a connection string in C#

To get a connection string from .config file…

Add a reference to System.Configuration and then use the following…

using System.Configuration;
...
var con = ConfigurationManager.ConnectionStrings["connection name"].ConnectionString;