PyDeveloper
PyDeveloper

Reputation: 319

Can't find the reference to a library

Trying to shadow copy file from path A to Path B but it keep throwing me error message

{"Could not load file or assembly 'AlphaVSS.x64, Version=1.4.0.0, Culture=neutral, PublicKeyToken=959d3993561034e3' or one of its dependencies. An attempt was made to load a program with an incorrect format."}

Code i try with it

  Public Sub copyFile(baza As String, destination As String)

        If IO.File.Exists(baza) Then

            Dim myFileInfo As FileInfo
            myFileInfo = New FileInfo(baza)

            Dim _volume As String = myFileInfo.Directory.Root.Name

            Dim _vssImplementation As IVssImplementation = VssUtils.LoadImplementation()
            Dim _backup As IVssBackupComponents = _vssImplementation.CreateVssBackupComponents()

            _backup.InitializeForBackup(Nothing)

            _backup.GatherWriterMetadata()

            _backup.SetContext(VssVolumeSnapshotAttributes.Persistent Or VssVolumeSnapshotAttributes.NoAutoRelease)
            _backup.SetBackupState(False, True, Alphaleonis.Win32.Vss.VssBackupType.Full, False)

            Dim MyGuid01 As Guid = _backup.StartSnapshotSet()
            Dim MyGuid02 As Guid = _backup.AddToSnapshotSet(_volume, Guid.Empty)

            _backup.PrepareForBackup()
            _backup.DoSnapshotSet()

            _backup.ExposeSnapshot(MyGuid02, Nothing, VssVolumeSnapshotAttributes.ExposedLocally, "L:")

            Dim sVSSFile2 As String = baza.Replace(_volume, "L:\")

            If (File.Exists(sVSSFile2)) Then
                System.IO.File.Copy(sVSSFile2, destination + "\" + System.IO.Path.GetFileName(baza), True)

                For Each prop As VssSnapshotProperties In _backup.QuerySnapshots
                    If (prop.ExposedName = "L:\") Then
                        Console.WriteLine("File found !")
                        _backup.DeleteSnapshot(prop.SnapshotId, True)
                    End If
                Next

                _backup = Nothing

                Console.WriteLine("File copied")

            Else

                Console.WriteLine("------------------------------------------")
                Console.WriteLine("File not exist: " + baza)

            End If
        End If
    End Sub

I have the VSS nuget package and references

enter image description here

It throws error on line

Dim _vssImplementation As IVssImplementation = VssUtils.LoadImplementation()

Upvotes: 1

Views: 558

Answers (3)

Mustafa Kuru
Mustafa Kuru

Reputation: 11

Manual solution of the problem.

Open the *.csproj file with your own notepad

 <PropertyGroup Condition="............">
.....other props...
<!-- Just add the following line here -->
<Prefer32Bit>false</Prefer32Bit>

 </PropertyGroup>

Upvotes: 0

Visual Vincent
Visual Vincent

Reputation: 18310

An attempt was made to load a program with an incorrect format

This message means you're trying to load an executable (.dll, .exe, etc.) which cannot be run by the process (or by Windows itself depending on the situation).

The most common cause for this error is trying to load 32-bit code into a 64-bit process (or vice versa). 64-bit processes can only run 64-bit code, and 32-bit processes can only run 32-bit code.

Since it is the AlphaVSS.x64 assembly that's faulting, your project is most likely compiled into a 32-bit executable. You should either remove this reference or change your project to target x64 only (note that if you do the latter you won't be able to run your application on 32-bit machines).

Upvotes: 0

djv
djv

Reputation: 15774

Read the documentation. There are requirements when using the package

  1. Visual C++ 2017 Redistributables must be installed on the machine running any application using AlphaVSS

  2. Note that your application must be built using "Prefer 32-bit" unchecked if the Platform Target is set to Any CPU. Also note that your application must be built for 64-bit to work on a 64-bit system (or Any CPU with Prefer 32-bit off), and in 32-bit to work on a 32-bit system.

I initially had the same error as you, until I unchecked "Prefer 32-bit", then that line of code worked.

Upvotes: 7

Related Questions