Fix - just ignore it
I had the same problem in my .NET Framework app and I found out that I can... just ignore that exception and click continue in debug -> goes further and session is opened.
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'XXXXX' threw an exception.
using (Session sess = new Session())
{
//sess.SessionLogPath = @"C:\GUR";
sess.Open(sessionOptions);
//Get Ftp File
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode -
transferOptions.FilePermissions = new FilePermissions(0666); //Permissions applied to remote files. 0666 is read/write
transferOptions.PreserveTimestamp = false; //Set last write time of
transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
TransferOperationResult transferResult;
transferResult = sess.PutFiles(fileName, ConfigurationManager.AppSettings["SFTPToPath"], false, transferOptions);
transferResult.Check();
}
mscorlib.dll!System.IO.__Error.WinIOError(int errorCode, string maybeFullPath) + 0x321 bytes
mscorlib.dll!System.IO.FileStream.Init(string path, System.IO.FileMode mode, System.IO.FileAccess access, int rights, bool useRights, System.IO.FileShare share, int bufferSize, System.IO.FileOptions options, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy, bool useLongPath) + 0x477 bytes
mscorlib.dll!System.IO.FileStream.FileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) + 0x54 bytes
mscorlib.dll!System.IO.File.Open(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) + 0x25 bytes
WinSCPnet.dll!WinSCP.SessionLogReader.OpenLog() + 0xd1 bytes
WinSCPnet.dll!WinSCP.SessionLogReader.DoRead() + 0x64 bytes
WinSCPnet.dll!WinSCP.SessionLogReader.Read(WinSCP.LogReadFlags flags) + 0x86 bytes
WinSCPnet.dll!WinSCP.CustomLogReader.TryWaitForNonEmptyElement(string localName, WinSCP.LogReadFlags flags) + 0x5f bytes
WinSCPnet.dll!WinSCP.CustomLogReader.WaitForNonEmptyElement(string localName, WinSCP.LogReadFlags flags) + 0x29 bytes
WinSCPnet.dll!WinSCP.CustomLogReader.WaitForNonEmptyElementAndCreateLogReader(string localName, WinSCP.LogReadFlags flags) + 0x29 bytes
WinSCPnet.dll!WinSCP.Session.Open(WinSCP.SessionOptions sessionOptions) + 0x775 bytes
// Connect
session.Open(sessionOptions);
Session.Open is returning this error.
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\Users\Admin\AppData\Local\Temp\wscp24C0.0223CC89.tmp' because it is being used by another process.
private bool uploadFile(string path, string hostName, string username, string password)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = hostName,
UserName = username,
Password = password,
SshHostKeyFingerprint = "ssh-rsa 1024 **:**:**:**:20:8f:95:69:12:f5:52:**:**:**:**:**"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = session.PutFiles(path, "/", false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
return true;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return false;
}
}