# How to properly reference an assembly

**URL:** <https://forums.powershell.org/t/how-to-properly-reference-an-assembly/5000>\
**Category:** PowerShell Help\
**Created:** [October 12, 2015, 3:57am UTC](https://forums.powershell.org/t/how-to-properly-reference-an-assembly/5000 "2015-10-12T03:57:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![windows-liveuser120](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@windows-liveuser120](https://forums.powershell.org/u/windows-liveuser120)\
**Post date:** [October 12, 2015, 3:57am UTC](https://forums.powershell.org/t/how-to-properly-reference-an-assembly/5000/1 "2015-10-12T03:57:13Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![windows-liveuser120](https://avatars.discourse-cdn.com/v4/letter/w/96bed5/32.png) [@windows-liveuser120](https://forums.powershell.org/u/windows-liveuser120)\
**Post date:** [October 12, 2015, 5:40am UTC](https://forums.powershell.org/t/how-to-properly-reference-an-assembly/5000/2 "2015-10-12T05:40:37Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/how-to-properly-reference-an-assembly/5000/3 "2024-05-16T20:44:41Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


