StyleCop was just released!

Programmatically Open ShapeFiles with ArcObjects

6. August 2010 13:42 by Scott in   //  Tags:   //   Comments (61)


Along with my last few posts, I received an Error when trying to open shapefiles with Arcobjects. Thought I would post the problem incase someone else ran into it.

Here is the code to open shapefiles with Arcobjects.

IWorkspaceFactory workspaceFactoryShape = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); //creates a shape factory to pull the shape and create the feature class.
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactoryShape.OpenFromFile(System.IO.Path.GetDirectoryName(file), 0); //Opens the Shape file.
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(file)); //opens the featurclass from the shapefile


Two things to point out.

  1. The openfromFile method will throw the error:  HRESULT: 0x80040258 if you don’t get the directory in which the shape files are located.  Its not looking for the actual .shp file.  Its looking for where the shape files reside. 
  2. The OpenFeatureclass method is where you give just the file name of the shape file.  Don’t include the .shp extenstion nor the file path.  Remember it already has the file path I stated in the first point.

Hope this helps someone out! I know I struggled with it for about 2 hours…

If you liked this post, please be sure to subscribe to my RSS Feed.

How to Download a File in C#

4. August 2010 13:32 by Scott in   //  Tags:   //   Comments (0)

The first example shows how to download a file synchronously and the second, asynchronously.  Just a quick post.  Thought it would help someone out that.

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("", @"c:\file.txt");

This one shows how to download a file in C# asynchronously.

private void btnDownload_Click(object sender, EventArgs e)
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  webClient.DownloadFileAsync(new Uri(""), @"c:\myfile.txt");

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  progressBar.Value = e.ProgressPercentage;

private void Completed(object sender, AsyncCompletedEventArgs e)
  MessageBox.Show("Download completed!");
If you liked this post, please be sure to subscribe to my RSS Feed.

How to publish a ArcMap to the ArcGIS Server…

4. August 2010 10:02 by Scott in   //  Tags:   //   Comments (46)

So after tons of searching and bothersome problems, It took a while to find code, but none could be found.  So I wanted to make sure the end example was easily findable on the internet next time.  Hope this saves some time of another programmer out there!

This code will publish an .MXD map to the ArcGIS server.  And turn it on to be used right away.

 //The following C# code shows how to connect to the ArcGIS Server called "     ",
        //and use the IServerObjectConfiguration interface to set the properties of
        //a new server object configuration. The new configuration is created and
        // added to the server with the 
        //CreateConfiguration and AddConfiguration methods on IServerObjectAdmin.
        /// <summary>
        /// This publishes a Map to the ArcGIS Server
        /// </summary>
        /// <param name="serverMachineName">Servername</param>
        /// <param name="serviceName">Service name in which you want to call it</param>
        /// <param name="pathToMap">File Path to the .mxd map.</param>
        /// <param name="format">Which format you would like.  Look in code.</param>
        public static void AddMapService(string serverMachineName, string serviceName, string pathToMap, int format)
            // Initialize ESRI licenses
            IAoInitialize aoInit = new AoInitializeClass();

            //define paths for the onlineresource property in the extension properties
            string resource = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WMSServer";
            string resourceKML = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/KMLServer";
            string resourceWCS = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WCSServer";
            string resourceWFS = "http://" + serverMachineName + "/ArcGIS/services/" + serviceName + "/MapServer/WFSServer";

            //  Connect to the ArcGIS server called  serverMachineName (string) .   
            IGISServerConnection pGISServerConnection = new GISServerConnectionClass();
            // example :  pGISServerConnection.Connect("llv38-tasc65688");   

            // create the new configuration
            IServerObjectAdmin pServerObjectAdmin = pGISServerConnection.ServerObjectAdmin;
            IServerObjectConfiguration pConfiguration = pServerObjectAdmin.CreateConfiguration();
            IServerObjectConfiguration2 pConfiguration2 = (IServerObjectConfiguration2)pConfiguration;
            IServerObjectConfiguration3 pConfiguration3 = (IServerObjectConfiguration3)pConfiguration;

            // set the   General Configuration Settings
            pConfiguration.Name = serviceName;           //  the name of this configuration
            pConfiguration.TypeName = "MapServer";   // the type of server object to be created
            pConfiguration.IsPooled = true;
            pConfiguration.MinInstances = 1;
            pConfiguration.MaxInstances = 2;
            pConfiguration.WaitTimeout = 60;
            pConfiguration.UsageTimeout = 600;
            pConfiguration.StartupType = esriStartupType.esriSTAutomatic;
            pConfiguration.IsolationLevel = esriServerIsolationLevel.esriServerIsolationHigh;

            //  Set the configuration Properties of the MapServer Object
            IPropertySet pProps = pConfiguration.Properties;
            pProps.SetProperty("FilePath", pathToMap);    // required property
            pProps.SetProperty("OutputDir", "c:\\arcgisserver\\arcgisoutput");
            string virtualOutDir = " http://" + serverMachineName + "/arcgisoutput";
            pProps.SetProperty("VirtualOutputDir", virtualOutDir);
            pProps.SetProperty("MaxImageHeight", "2048");
            pProps.SetProperty("MaxRecordCount", "500");
            pProps.SetProperty("MaxBufferCount", "100");
            pProps.SetProperty("MaxImageWidth", "2048");
            pConfiguration.Properties = pProps;

            //  Set the info segment of the MapServer Object  properties
            IPropertySet info = pConfiguration2.Info;
            info.SetProperty("WebEnabled", "true");
            info.SetProperty("WebCapabilities", "Map,Query,Data");
            pConfiguration2.Info = info;

            //  Set the recycle properties of the MapSrver object
            IPropertySet pProp = pConfiguration.RecycleProperties;
            pProp.SetProperty("StartTime", "1:00 AM");    //   start recycling at midnight
            pProp.SetProperty("Interval", "86400");      //  every 24 hours
            pConfiguration.RecycleProperties = pProp;

            bool enabled;
            if (format == 1 || format == 15)
            {   //Set WMS extension Properties
                pConfiguration2.set_ExtensionEnabled("WmsServer", true);

                IPropertySet pExtensionProps = pConfiguration2.get_ExtensionProperties("WmsServer");
                pExtensionProps.SetProperty("OnlineResource", resource);
                pExtensionProps.SetProperty("Name", "WMS");
                pExtensionProps.SetProperty("Title", serviceName);
                // pConfiguration2.set_ExtensionProperties("WmsServer", pExtensionProps);

                IPropertySet pProp2 = pConfiguration2.get_ExtensionInfo("WmsServer");
                pProp2.SetProperty("WebEnabled", "true");
                pProp2.SetProperty("WebCapabilities", "Map,Query,Data");
                pConfiguration2.set_ExtensionInfo("WmsServer", pProp2);

            if (format == 2 || format == 15)
            {    //  Set KML extension properties
                pConfiguration2.set_ExtensionEnabled("KMLServer", true);

                IPropertySet pPropKML = pConfiguration2.get_ExtensionInfo("KMLServer");
                pPropKML.SetProperty("WebEnabled", "true");
                pPropKML.SetProperty("WebCapabilities", "SingleImage,SeparateImages,Vectors");
                pConfiguration2.set_ExtensionInfo("KMLServer", pPropKML);

                IPropertySet pExtensionPropKML = pConfiguration2.get_ExtensionProperties("KMLServer");
                pExtensionPropKML.SetProperty("ImageSize", "1024");
                pExtensionPropKML.SetProperty("FeatureLimit", "1000000");
                pExtensionPropKML.SetProperty("Dpi", "96");

                pExtensionPropKML.SetProperty("MinRefreshPeriod", "30");
                pExtensionPropKML.SetProperty("UseDefaultSnippets", "false");
                //       pConfiguration2.set_ExtensionProperties("KMLServer", pExtensionPropKML);

            if (format == 4 || format == 15)
            {   //Set WCS extension Properties
                pConfiguration2.set_ExtensionEnabled("WcsServer", true);

                IPropertySet pExtensionPropsWCS = pConfiguration2.get_ExtensionProperties("WcsServer");
                pExtensionPropsWCS.SetProperty("OnlineResource", resourceWCS);
                pExtensionPropsWCS.SetProperty("Name", "WCS");
                pExtensionPropsWCS.SetProperty("Title", serviceName);
                // pConfiguration2.set_ExtensionProperties("WcsServer", pExtensionPropsWCS);

                IPropertySet pPropWCS = pConfiguration2.get_ExtensionInfo("WcsServer");
                pPropWCS.SetProperty("WebEnabled", "true");
                pConfiguration2.set_ExtensionInfo("WcsServer", pPropWCS);

            if (format == 8 || format == 15)
            {   //Set WFS Extension Properties
                pConfiguration2.set_ExtensionEnabled("WfsServer", true);

                IPropertySet pExtensionPropsWFS = pConfiguration2.get_ExtensionProperties("WfsServer");
                pExtensionPropsWFS.SetProperty("OnlineResource", resourceWFS);
                pExtensionPropsWFS.SetProperty("Name", "WFS");
                pExtensionPropsWFS.SetProperty("Title", serviceName);
                pExtensionPropsWFS.SetProperty("AppSchemaURI", resourceWFS);
                pExtensionPropsWFS.SetProperty("AppSchemaPrefix", serviceName);
                //     pConfiguration2.set_ExtensionProperties("WfsServer", pExtensionPropsWFS);

                IPropertySet pPropWFS = pConfiguration2.get_ExtensionInfo("WfsServer");
                pPropWFS.SetProperty("WebEnabled", "true");
                pConfiguration2.set_ExtensionInfo("WfsServer", pPropWFS);

            //' add the configuration to  the server
            pServerObjectAdmin.StartConfiguration(serviceName, "MapServer");
        }  //  end method
If you liked this post, please be sure to subscribe to my RSS Feed.

How to create Feature Layer from a Shapefile in C#

30. July 2010 09:38 by Scott in   //  Tags:   //   Comments (11)

I hate when I can't find things easily on the web.  Things are soo buried on the ESRI website its ridiculous.  So I am going to start posting simple solutions and complaints if I find any.  So thank you ESRI for being so hard to code for.

Complaint Number 1 about ESRI's site - When giving examples or even Class Objects, you don't show the namespaces.  That leaves us developers looking for where classes are buried in several different namespaces.  This is a problem that needs to be fixed.

So on to the example:

using ESRI.ArcGIS.Geodatabase;

IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(location, 0);
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapefilename);
Console.WriteLine("There are {0} features in the {1} feature class", featureClass.FeatureCount(new QueryFilterClass()), featureClass.AliasName);
Make Note that the variable location is does NOT include the file name.
For example: "C:\Users\Desktop\Newfolder" and NOT "C:\Users\Desktop\Newfolder\blah.shp"

Thats it!

Hope this will help someone down the road.  It took me about an hour to find it on the ESRI website.

If you liked this post, please be sure to subscribe to my RSS Feed.

How to Boost Performance of Your ASP.NET Web App.

1. July 2010 20:41 by Scott in   //  Tags:   //   Comments (50)

I have a website over at with over 15k users and it was getting bogged down a ton by database hits.  I needed to speed the site up because of the DB calls.  So I threw a ton of stuff into Cache, but that didn't speed the site up enough.  So I looked for faster ways to do these things and wanted to give a shout out to this site that helped drastically.

Here is the link to the Article. I suggest you take a look at it if you wanted to do the same. 



If you liked this post, please be sure to subscribe to my RSS Feed.

How to Remove a Workplace or Local Path with TFS

2. March 2010 10:16 by Scott in   //  Tags: , , , ,   //   Comments (0)

I had an error in Team Foundation server saying something like one of my folders were already mapped to this local path from another server.  So I looked around and couldn't find a fix for it on the internet.  I had to figure this one out my self so I thought I would share my findings.

In order to remove this mapping from TFS because maybe you deleted the folder on your desktop a long time ago.  You need to go to File -> Source Control -> Manage Workplaces. 

From there you have the option to remove any mappings you wish in TFS.  Just thought I would throw that out there.  Took a while for me to find it and thought I should share it.

If you liked this post, please be sure to subscribe to my RSS Feed.

Yet Another Forum Controls: Top Posts and Last Post Controls

15. June 2009 09:14 by Scott in   //  Tags: , , , , ,   //   Comments (6)

In my time of developing apps, I love YAF and would love to see more development for it. So in helping out with the Forum, I wanted to contribute and I imagine I will be working on more controls later down the road for YAF.

I built these controls for the third party application I host at  It is used for the online game at  Its a GREAT game which challenges your intellect.  It is completely text based so requires a lot of thinking.  If you ever decided to give it a try, look me up.  Im around the game and the site.  I am hooked on it.

In using these controls, I ask that you help out with YAF. Not just in downloading these controls, but building your own stuff!

To YAF/Control Developers: There are two problems with these controls. I am using LINQ as a segway with connecting to the database. So if you would like to do something different, please replace the "DataContexts" with something else for connecting to the database. Along with the Execute Query part needs to be replaced as well.
THE OTHER PROBLEM: YAF URL Re-directions STINK. They just don't work outside the forum. So if your posting any of this on a page other than the default page, the page needs to be replaced with the default page.

To All other control users: These controls can either be spit out as tables or Lists. Go to the code behind and comment out the Table, but by default it displays the list.

1. PostCount: Default = 10, but can be set to anything.
2. BoardName: Default is no board name. Its just all boards, but can be set to any board name.
3. Class: This is the CSSClass.
4. Row class and alternating Class can be un-commented and implemented on each control.

Here are the controls.

Put these controls into the forum/controls folder and drag and drop them anywhere on the site. (1.67 kb) (1.79 kb)

If you liked this post, please be sure to subscribe to my RSS Feed.

Medium Difficulty Math Problem

30. April 2009 05:54 by Scott in   //  Tags:   //   Comments (4)

I haven't done real math in a couple of years in college.  An haven't done true algebra for almost a decade now, but I was just confronted with a real math problem I didn't know how to do.  I actually had to read off someone else's code to figure it out and see whats going on.  I suspect others have might have this same problem so I decided to brush up on my math skills a bit and expect almost every programmer to do the same.

The problem I had is this:

I know there is a name for this problem, but it escapes me at this moment.

I have k, a static number that never changes is multiplied by 50% to 150 percent on a random basis.  The answer will be k multiplied by 50% to 150% randomly and I have to figure out what 'k' is through the answers I get.

k * .50-1.50 = 15000
k * .50-1.50 = 12000

So if I have the equation above, I have to find where 15000/(.5 - 1.5) and 12000/(.5 - 1.5) intersect. The intersection point(s) could be k. I also get the chance to add as many (answers) to the equation as possible so I can define my answer more and more if I would like.

So here is the problem.  If I do have the numbers 15000 and 12000, what is k?

Good Luck and tell me in the comments how you did it and the actual answer(s).  I will post the answer(s) on Monday.

Have Fun!

If you liked this post, please be sure to subscribe to my RSS Feed.

Top Post and Top Comment Widgets for BlogEngine.Net

27. April 2009 09:00 by Scott in   //  Tags: ,   //   Comments (3)

Recently I found a post on Stackoverflow for someone looking for a Top Posts widget.  I knew I had created one, but not yet posted it.  So here is the post.

Download the Zip file and add the extension to the extensions directory and add the widgets to the widgets directory.  The top posts widget will only update every 30 minutes because it wouldn't need to be updated so frequently.

So here they are:

The Top Posts widget allows you to view how many times each post was viewed.

The Top comments widget allows you to see how many comments were posted on each post.

Hope it helps everyone out and as you can see on my right side, it works on my machine! (17.46 kb)


If you liked this post, please be sure to subscribe to my RSS Feed.

Blogging Fame and Fortune Isn't Easy!!

3. April 2009 03:58 by Scott in   //  Tags: ,   //   Comments (9)

You know you have many great bloggers in the world.  There is no need to go down a list of them, just to understand that they are out there.

I have been thinking for a while now, why hasn't my numbers come anywhere close to what other bloggers have achieved?  Is it the fact that its such a saturated field?  Is it the fact that Im not old school?  Is it the fact that I just started up a year ago?  Is it the fact that I don't write good content?  What makes people subscribe and unsubscribe to a variety of blogs.  I wish I had an answer to these many questions, but I don't. I wish my blog could reach as many people as blogs with 100,000 readers, but it doesn't. What can I do to reach those numbers? What can anyone do to reach those numbers?

Photo By deanj


This is like a cry for help.  I write about a small niche.  My thoughts are the niche, but I still don't get the amount of readers one desires to make an impact.  Maybe my name isn't out there as much.  Maybe Im just not a good writer.  I did actually start this blog to become a better writer.  I see those 90 day trial books that say something like "5000 readers in less than 90 days for only $15.00".  I don't believe in those things and will never start to buy into them. That begs the question tho, is it truly plausible to get 5k readers in 90 days?  Ya, maybe but you would have to advertise your blog like crazy on all the popular search engines and social sites of life.

So what make a blog so popular?  I have no clue, but its upsetting after blogging for so long, I still don't have the reach I think I should have.  I guess im not that interesting to think of good topics.  I try to be personal, but it just doesn't work out well I guess.

Im not begging for mercy or like "go add me now!".  But I am wondering what it really takes to be a good and popular blogger because I am not even close yet.

Thanks for listening to my grunts.  If you have any insight at all, let me know.  I am not looking for the codinghorror link here that says to keep moving and jabbing.  I am looking for some no non-sense advice.  What does it take?

If you liked this post, please be sure to subscribe to my RSS Feed.