Search This Blog

Tuesday, June 26, 2018

MSDN general guidelines for debugging with Visual Studio

Taken from https://referencesource.microsoft.com/setup.html

         You can configure Visual Studio to automatically step over system, framework, and other non-user calls and collapse those calls in the call stack window. The feature that enables or disables this behavior is called Just My Code. This topic describes how to use Just My Code in C#, Visual Basic, C++, and JavaScript projects.
          To debug .NET Framework source, you must have access to debugging symbols for the code. You also need to enable stepping into .NET Framework source.

          You can enable .NET Framework stepping and symbol downloading in the Options dialog box. When you enable symbol downloading, you can choose to download symbols immediately or just enable the option for later downloading. If you do not download the symbols immediately, symbols will be downloaded the next time that you start debugging your application. You also can do a manual download from the Modules window or the Call Stack window.

  • Require Source Files to Exactly Match The Original Version

Monday, June 25, 2018

Which C# version is my app using ?

Determining the version of the C# language an application uses is a function of 3 parameters: .NET framework being targeted, the C# version chosen from the available ones (project properties | build | advanced | language version), and visual studio version installed.

Here you find the table listing the available combinations, as of today, June 25th, 2018.

Unexpected results with no exceptions being thrown - apparently

If you are facing undesired results or strange behaviors, but are not spotting any exception in run time - make sure you checked all the Common Language Runtime Exceptions in Debug -> Exception Settings.

I was facing an InvalidOperationException due to unsafe cross thread access.
But it was only when I instructed VS to break on all exceptions from the CLR, then I finally got the hint of where/who was raising the exception.

Sunday, June 17, 2018

Are you merging your project DLLs and getting a Could not load file or assembly or one of its dependencies?

If you are facing the endeared "Could not load file or assembly or one of its dependencies" error, and you know your project merges its assemblies using ILMerge or alikes, make sure the dlls that is raising the exception was not accidentally left out the merge.

This is what happened in my case.
All I had to do was to add it to the merging, marking it with the <Ilmerge>True</Ilmerge> in the .csproj, like the others.

Monday, June 11, 2018

Devexpress Project Converter, Clickonce PreRequisite dependencies, GAC and the ensuing hours you have wasted...

We have run DeveExpress Project Converter in Visual Studio to upgrade our clickonce from 17.1.5 to 18.1.3. Problem was that after the conversion 2 dlls, DevExpress.Images and DevExpress.RichEdit.Export were added to the Application Files (Publish tab) list with Publish Status as PreRequisites. The impact of this is that deploying the app showed an error message stating dll ***** was required to exist in the Windows GAC folder.

The solution, therefore, was to change the Publish Status of these PreRequisite dlls (in our case, to Exclude, because we deploy all devexpress from an external zip, in an external process - but usually you will set this to Include). After that, publishing will generate a manifest that does not show these dlls with PreRequisite dependencies, and hence you are rid of the deployment installation GAC message.

Monday, April 30, 2018

Wrapper for SharpZipLib (C# compressing nuget package)

Wrapper I wrote for SharpZipLib:


using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.SharpZipLib.Core;

public static class SharpZipLibHelper
{
    //I wrapped the author's samples at https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples

    #region Functions

    private static bool _ignore(List<string> excludes, string fileName)
    {
        foreach (var exclude in excludes)
        {
            //ignore a group of files with a given extension if it was specified in the excludes list
            if (exclude.IndexOf("*") > -1)
            {
                var fileExtension = Path.GetExtension(fileName);
                var excludeExtension = Path.GetExtension(exclude);
                if (string.Equals(excludeExtension, fileExtension, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }
            //ignore a specific file if it was specified in the excludes list
            else
            {
                if (string.Equals(fileName, exclude, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }
        }

        return false;
    }

    private static void _compressFolder(string path, ZipOutputStream zipStream, int folderOffset, List<string> excludes = null)
    {
        var files = Directory.GetFiles(path);

        foreach (string filename in files)
        {
            FileInfo fi = new FileInfo(filename);

            if (excludes == null) excludes = new List<string>();
            if (_ignore(excludes, fi.Name)) continue;

            string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
            entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

            // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
            // A password on the ZipOutputStream is required if using AES.
            //   newEntry.AESKeySize = 256;

            // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
            // you need to do one of the following: Specify UseZip64.Off, or set the Size.
            // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
            // but the zip will be in Zip64 format which not all utilities can understand.
            //   zipStream.UseZip64 = UseZip64.Off;
            newEntry.Size = fi.Length;

            zipStream.PutNextEntry(newEntry);

            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();
        }
        string[] folders = Directory.GetDirectories(path);
        foreach (string folder in folders)
        {
            _compressFolder(folder, zipStream, folderOffset, excludes);
        }
    }

    #endregion

    #region Methods

    public static void CreateZipFromFolder(string folderToZipPath, string zipFullFilePath, List<string> excludes = null)
    {
        FileStream fsOut = File.Create(zipFullFilePath);
        ZipOutputStream zipStream = new ZipOutputStream(fsOut);

        zipStream.SetLevel(9); //0-9, 9 being the highest level of compression

        //zipStream.Password = password;  // optional. Null is the same as not setting. Required if using AES.

        // This setting will strip the leading part of the folder path in the entries, to
        // make the entries relative to the starting folder.
        // To include the full path for each entry up to the drive root, assign folderOffset = 0.
        int folderOffset = folderToZipPath.Length + (folderToZipPath.EndsWith("\\") ? 0 : 1);

        _compressFolder(folderToZipPath, zipStream, folderOffset, excludes);

        zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
        zipStream.Close();
    }

    public static void ExtractZipFile(string zipFile, string destinationFolder, List<string> excludes = null, string password = null)
    {
        ZipFile zf = null;
        try
        {
            FileStream fs = File.OpenRead(zipFile);
            zf = new ZipFile(fs);
            if (!String.IsNullOrEmpty(password))
            {
                zf.Password = password;     // AES encrypted entries are handled automatically
            }
            foreach (ZipEntry zipEntry in zf)
            {
                // Ignore directories
                if (!zipEntry.IsFile)
                    continue;

                if (excludes != null && zipEntry.IsFile)
                {
                    if (_ignore(excludes, zipEntry.Name)) continue;
                }

                String entryFileName = zipEntry.Name;
                // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
                // Optionally match entry names against a selection list here to skip as desired.
                // The unpacked length is available in the zipEntry.Size property.

                byte[] buffer = new byte[4096];     // 4K is optimum
                Stream zipStream = zf.GetInputStream(zipEntry);

                // Manipulate the output filename here as desired.
                String fullZipToPath = Path.Combine(destinationFolder, entryFileName);
                string directoryName = Path.GetDirectoryName(fullZipToPath);
                if (directoryName.Length > 0)
                    Directory.CreateDirectory(directoryName);

                // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                // of the file, but does not waste memory.
                // The "using" will close the stream even if an exception occurs.

                using (FileStream streamWriter = File.Create(fullZipToPath))
                {
                    StreamUtils.Copy(zipStream, streamWriter, buffer);
                }
            }
        }
        finally
        {
            if (zf != null)
            {
                zf.IsStreamOwner = true; // Makes close also shut the underlying stream
                zf.Close(); // Ensure we release resources
            }
        }
    }

    #endregion
}

Sunday, February 4, 2018

Adding files and folders to a click once installation

Having trouble in adding specific files to your click once installation ? Morover, you want them to be placed under a specific folder ?

Here are the steps I followed that finally did it:

  1. You need to create the folder/file structure just like you want it to exist in your installation - in your visual studio project. For example, I needed two folders - x86 and x64, and the same  SQLite.Interop.dll under both
  2. Right click in your project, go to the Publish tab, then click in Application Files. Scroll down (they should be showing as the last items) to your added files, and make sure you change the Publish Status from Include (Auto) to Include. 
Should work now.