mercoledì 4 giugno 2014

Zipping files/folders and then delete the source.

C# .NET Framework 4.51

In my console application, I want to zip a single file or folder and then delete the source.
For this purpose, I use the ZipFile object for zipping folders and ZipArchive object for zipping files.
The problem is that after zip, I have an exception when I delete the source, because folders or files are in use, so I have resolved with asynchronous programming using a Task object (and arguments) with Async and Await.

private async void ExecuteCommand()
        {
// Arguments for async task.
var arguments = new List<string> {zipOutput, sourceName, internalOperation};

var returnedTask = DoTaskAsync(arguments);
bool taskResult = await returnedTask;

if (taskResult)
{
    if (internalOperation.Equals("folder"))
    {
        // Remove the read-only attribute.
        var di = new DirectoryInfo(destinationRepositoryName);

        foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
            file.Attributes &= ~FileAttributes.ReadOnly;

            Directory.Delete(sourceName, true);
     }
     else
         File.Delete(sourceName);

     Console.WriteLine(Environment.NewLine);
     Console.WriteLine("End zipping file -> " + zipOutput);
 }
}

// Signature specifies Task<TResult>
private async Task<bool> DoTaskAsync(IReadOnlyList<string> arguments)
{
    try
    {
         Console.WriteLine(Environment.NewLine);
         if (arguments != null)
         {
             Console.WriteLine("Create zipping file -> " + arguments[0]);
                   
             if (arguments[2].Equals("folder"))
                 ZipFile.CreateFromDirectory(arguments[1], arguments[0], CompressionLevel.Fastest, true);
             else
             {
                 using (var archive = ZipFile.Open(arguments[0], ZipArchiveMode.Create))
                 {
                     var fileInfo = new FileInfo(arguments[1]);
                     archive.CreateEntryFromFile(arguments[1], fileInfo.Name);
                 }
              }
              return true;
         }
         Console.WriteLine("Can't create zip file because arguments are null.");
               
         return false;
     }
     catch (Exception ex)
     {
         return false;
     }
 }

See you soon!

venerdì 4 aprile 2014

Developer Conference 2014

It's a great conference dedicated to .NET developers, for developing client and web applications, Windows Phone and Windows 8.x apps.

http://www.developerconference.it/events/2014/default.aspx

mercoledì 19 marzo 2014

LINQ to entities - Error occured update the entries

C# .NET Framework 4.0, Linq to entities

I have an application that uses Linq to entities for accessing to a SQL Server 2008 database, naturally this application works fine. Therefore, I deploy it in a system where there is SQL 2005 express and not SQL Server 2008 and my application gives the follow exception:

Message: An error occurred while updating the entries. See the inner exception for details.

The solution is:

  • Open the entity data model, for example Model.edmx, with a XML Editor.
  • Change the attribute ProviderManifestToken (Schema element -> <Schema)
          from
      ProviderManifestToken="2008"
      to
      ProviderManifestToken="2005"
  • Rebuild and re-deploy the assembly.

It works fine for me.

venerdì 7 febbraio 2014

WorkShop Android a Chioggia

Ciao a tutti, un'iniziativa interessante per chi vuol muovere i primi passi con Android.

Se siete interessati ecco il link: http://goo.gl/SDV354

giovedì 30 gennaio 2014

Community Days 2014

Organized by Italian community and user group devoted to Microsoft products and technologies, to offer study days in the form of technical conferences.


 Community Days 2014

martedì 14 gennaio 2014

Calling a WCF service through a proxy server.

C# .NET Framework 4.0

I have an application client that calls a WCF service and in this client uses a proxy for the internet connection, so when I call my service I must first provide the proxy authentication.
I have created a class that implements the IWebProxy interface:

namespace Utililty {
   public class ProxyConfig : IWebProxy {

        public ICredentials Credentials {
            get
            {
                using (var context = new AOEntities())
                {
                    var query =
                        from data in context.ConnectionParameters
                        where data.ServerName.Equals("Proxy")
                        select data;

                    if (query.Any())
                    {
                        var parameters = query.FirstOrDefault();
                        if (parameters != null) return new NetworkCredential(parameters.UserName, parameters.Password);
                    }

                    return null;
                }               
            }
            set { }
        }

        public Uri GetProxy(Uri destination) {
            return WebRequest.GetSystemWebProxy().GetProxy(destination);
        }

        public bool IsBypassed(Uri host) {
            return WebRequest.GetSystemWebProxy().IsBypassed(host);
        }       
    }
}

In this example, I retrieve the credentials from a SQL Server database, but if you prefer you can put in the application configuration file.

Then in the application configuration file (app.config), I have added this section:

 <system.net>
   <defaultProxy>
     <module type=" Utililty.ProxyConfig, Utililty"/>
   </defaultProxy>
 </system.net>

and the attribute useDefaultWebProxy="trueat the WCF binding configuration.

The ProxyConfig object will have call automatically before your call to service.

Peace & Love!

martedì 17 dicembre 2013

Merge several TIFF images to single multipage TIFF

C# .NET Framework 4.0

In my application, I have wanted to merge and compress several TIFF images to single multipage TIFF, for this purpose I used the framework Bitmap object with EncoderParameters like explained in this link http://stackoverflow.com/questions/3478292/whats-the-recommended-tiff-compression-for-color-photos/3480411#3480411 . Unfortunately, this solution was very slow! 3’:20’’ for merging of 1420 images.


So I searched in the web and I found this awesome solution http://bitmiracle.com/libtiff/help/merge-several-tiff-images-to-single-multipage-tiff.aspx : the time for merging images has dropped to 26’’! 

That’s miracle!