How to properly reference an assembly

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of Kawika Moss Kawika Moss 1 year, 9 months ago.

  • Author
    Posts
  • #30656
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    I've written a custom method in C# and I'm trying to use it in a powershell function, the issue I'm seeing at this point is, I'm using an assembly that I had to reference in VS and I'm not sure how to make a reference to it, properly, in powershell.

    $Assem = ("C:\Delimon.Win32.IO\Delimon.Win32.IO.dll, Version=4.0.4764.1936, Culture=neutral, PublicKeyToken=6f601db60ebd9657")
    
    $Source = @"
    using System;
    using System.Collections.Generic;
    using Delimon.Win32.IO;
    
    namespace LongFileExt
    {
        public class Truncate
        {
            public static List Extension(string originalPath, int extLength)
            {
                // Only get files that begin with the letter "c."
                string[] dirs = Directory.GetFiles(originalPath, "*.*", SearchOption.AllDirectories);
                List returnValue = new List();
    
                foreach (string dir in dirs)
                {
    
                    string ext = Path.GetExtension(dir);
                    int extCount = ext.Length;
    
                    if (extCount > 4)
                    {
    
                        string baseName = Path.GetFileNameWithoutExtension(dir);
                        string fileName = Path.GetFileName(dir);
                        string path = dir.Substring(0, dir.LastIndexOf(("\\")));
                        string newExt = ext.Substring(1, extLength);
                        string newFileName = path + "\\" + baseName + "." + newExt;
    
                        if (File.Exists(dir))
                        {
                            File.Move(dir, newFileName);
                        }
                        returnValue.Add(newFileName + ", " + baseName);
                    }
                }
                return returnValue;
            }
        }
    }
    "@
    
    Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
    [LongFileExt]::Truncate($exportPath,$extLength)
    

    And I am getting the following error:

    Add-Type : Could not load file or assembly 'C:\\Delimon.Win32.IO\\Delimon.Win32.IO.dll\, Version\=4.0.4764.1936\, Culture\=neutral\, PublicKeyToken\=6f601db60ebd9657' or one of its dependencies. The given assembly name 
    or codebase was invalid. (Exception from HRESULT: 0x80131047)
    At line:86 char:1
    + Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
        + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
    
  • #30670
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    I'm getting closer, I'm still getting an error, but it's different. Here is my latest code...

    $Assem = ("C:\Delimon.Win32.IO\Delimon.Win32.IO.dll")
    
    $Source = @"
    using System;
    using System.Collections.Generic;
    using Delimon.Win32.IO;
    
    namespace LongFileExt
    {
        public class Truncate
        {
            public static List Extension(string originalPath, int extLength)
            {
                // Only get files that begin with the letter "c."
                string[] dirs = Directory.GetFiles(originalPath, "*.*", SearchOption.AllDirectories);
                List returnValue = new List();
    
                foreach (string dir in dirs)
                {
    
                    string ext = Path.GetExtension(dir);
                    int extCount = ext.Length;
    
                    if (extCount > 4)
                    {
    
                        string baseName = Path.GetFileNameWithoutExtension(dir);
                        string fileName = Path.GetFileName(dir);
                        string path = dir.Substring(0, dir.LastIndexOf(("\\")));
                        string newExt = ext.Substring(1, extLength);
                        string newFileName = path + "\\" + baseName + "." + newExt;
    
                        if (File.Exists(dir))
                        {
                            File.Move(dir, newFileName);
                        }
                        returnValue.Add(newFileName + ", " + baseName);
                    }
                }
                return returnValue;
            }
        }
    }
    "@
    
    Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
    $return = [LongFileExt.Truncate]::Extension("D:\Case\0012_H12711_15Bb\NATIVE\H12711-0012-",25)
    

    Here is the error:

    Exception calling "Extension" with "2" argument(s): "Could not load file or assembly 'Delimon.Win32.IO, Version=4.0.4764.1936, Culture=neutral, PublicKeyToken=6f601db60ebd9657' or one of its dependencies. The system 
    cannot find the file specified."
    At line:47 char:1
    + $return = [LongFileExt.Truncate]::Extension("D:\Case\0012_H12711_15Bb\NATIVE\H12 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileNotFoundException
    

You must be logged in to reply to this topic.