Simple Trick To Publish ANY File Via ClickOnce

5. May 2010

I recently needed to deploy a WPF application with ClickOnce and have the deployment include an ActiveX DLL.  I couldn’t simply add a reference to the DLL in my solution. When I tried I Visual Studio said:

Please make sure that the file is accessible, and that it is a valid assembly or COM component.

Since I couldn’t add it as a reference – it wouldn’t show up in my “Application Files” list (Publish tab of project properties). If it isn’t in the Application Files list then we are unable to include it in the ClickOnce publish.

To get around this issue there is a simple trick you can do to push your ActiveX DLL (or any file type ) into a ClickOnce publish. Here are the steps:

 

  1. Rename your file to include a .txt extension. It should look something like “myassembly.dll.txt” or [name].[extension].txt’
  2. Add the renamed file to your solution.
  3. In Solution Explorer right-click the file, select properties, and change the Build Action to “Content”
  4. Now go back to Application Files on the Publish tab of the project properties. You should see your file in the list. If they aren’t already set – change the drop downs so that the values are as follows:

 

  • Publish Status – Include (Auto)
  • Download Group - (Required)
  • Hash - (Include)

 

After those step are complete, you have a small amount of coding to do. We’ll need to rename your file at runtime in order for the file to be usable. To do that we’ll need a bit of code from my last post.

 

if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) 
{
if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun)
{
//sneak the dll into the deployment by renaming it
if (File.Exists("myfile.dll.txt") && !File.Exists("myfile.dll"))
{
//rename to dll so it is accessible
File.Copy("myfile.dll.txt", "myfile.dll");
}
}
}

 

The code above basically checks to see if it is a ClickOnce deployment and if it is the first time the application has been ran on the current machine, and if so, renames the .txt file you publish with it’s real name so it can now be used as originally intended.

Using the method outlined in the post, you should be able to easily publish any file type with your ClickOnce deployed application.

Enjoy!

.NET, WPF ,

 Perform Custom Action - WPF ClickOnce Deployment

4. May 2010

 

If you’d like your WPF application deployed via ClickOnce to execute a custom action the first time the application is run you can use the following code:

 

if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
      if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun)
      {
                 // do some initialization here
       }
}

 

IsNetworkDeployed is a Boolean value indicating whether or not the application has been deployed via ClickOnce. IsFirstRun indicates whether this is the first time the application has been ran on the current machine. Be sure to add a reference to System.Deployment.

Enjoy!

.NET, WPF ,

 How To Run Performancing Ads on ASP.NET

3. August 2008

PerformancingAds Performancing Ads is a great new service that makes monetizing your blog super easy. The service allows you to set up 125 x 125 sized ads on your blog, by logging into their interface and setting up a new "region" on your site. You then download some PHP code, drop it on your site and start displaying ads. The only problem for .NET sites is that Performancing Ads officially supports only PHP sites.

Considering I run BlogEngine.NET, an ASP.NET blogging platform; it kind of sucked to sign up, set up a region, and then find out that only PHP sites are supported. I really wanted to try Performancing Ads on my site though, so I set out to find a way to do it, and I did! If you look to the right you'll see my Performancing Ads region. I've even sold an ad block already within the first couple of hours running it.

Here's what to do to get the ads running on your ASP.NET site:

  • 1.  Sign up with Performancing Ads.
  • 2.  Login, and create a new region for your site. (it's super easy no explaination needed)
  • 3. Download the PHP code they give you after setting up a region.
  • 4. Make a new folder in your sites root and put the PHP code you downloaded in it.
  • 5. Follow the instructions here to set up PHP on your Windows box.
  • 6. Add the Region ID # to the perfads method call in perafds.php file. It should look something like this: perfads(1514);
  • 7. Browse to perfads.php in your browser and make sure it works. If it doesn't you've got some fixing to do.
  • The first 7 steps are not unlike they would be if you were running a PHP site. The only difference is that you have to set up Windows and IIS to run PHP. Now comes the trick to embed the PHP generated Performancing Ads into your ASP.NET site. I tried embedding the ads in my page using an iframe and that worked great, up until the point someone actually clicks an ad. Since the ads are in an iframe, they will open in the frame showing only a small portion of the advertiser's landing page. There is javascript code you can put on a page to make it break out of iframes, but it would essentially need to sit on the advertisers pages - and that's not gonna happen. What I decided to do, was scrape my own page (the perfads.php page) using ASP.NET, and embed the resulting string into my ASP.NET page. So to do that you need to:
  • 8. Copy this very basic scraping function into your ASP.NET code behind: (you will need to Import System.NET and System.IO). 

  • public void GetHtmlPage(string strURL)
    {
           // the html retrieved from the page
           String strResult;
           WebResponse objResponse;
           WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
           objResponse = objRequest.GetResponse();
  •        // the using keyword will automatically dispose the object
          // once complete
          using (StreamReader sr =
          new StreamReader(objResponse.GetResponseStream()))
          {
             strResult = sr.ReadToEnd();
             // Close and clean up the StreamReader
             sr.Close();
          }
          litHTMLfromScrapedPage.Text = strResult;
         
  • 9. Put an ASP.NET Literal control with ID="litHTMLfromScrapedPage"  into your ASP.NET markup where you want the ads to appear.
  • 10. Call GetHtmlPage passing in the url to your perfads.php page from your page's PageLoad event. As you can see in the last line of code in the GetHtmlPage method, the scraped HTML is assigned to your Literal control's .Text property, essentially placing right into your ASP.NET page!

Done! Load your ASP.NET page and your Performancing Ads  should appear in your ASP.NET page! If you have any question's about setting up Performancing Ads on your BlogEngine.NET blog or any other ASP.NET website let me know and I'd be glad to help!

 

.NET