Tuesday 21 March 2017

Transfer PDF, Text file from one server to another server through code

We have a new requirement where we need to move the PDF file generated in one server and move it to another server
Create a new folder call Export in c drive and create text file to transfer in another server
After this try to execute this code, it will create sendTXTFile.bat file and execute it which will transfer in another server.
Static void UploadTxtFile(Args _args)
{
    TextIo            textIoFile;
    Set               permissionSet;
    FileName          filePath, filename;
    FileName          sharedLocation, batchFileName;
    InteropPermission interopPerm;
    System.Diagnostics.Process          process;
    System.Diagnostics.ProcessStartInfo processStartInfo;
    System.Exception netException;
    #File
    filePath       = 'c:\\Export'; //Create a Folder Export
    sharedLocation = 'Location';
    fileName       = filePath + '\\Abdulmajeed.txt';//used Txt file to transfer
    batchFileName  = filePath + '\\sendTxtFile.bat';
   
    try
    {
        if (System.IO.File::Exists(fileName))
        {
            permissionSet = new Set(Types::Class);
            permissionSet.add(new FileIoPermission(batchFileName, #io_write));
            CodeAccessPermission::assertMultiple(permissionSet);
            textIoFile = new TextIo(batchFileName , #io_write, 0);
            CodeAccessPermission::revertAssert();
            textIoFile.write(strFmt("net use %1 PWDxxxxxx /user:UserName", sharedLocation));
            textIoFile.write(strFmt("copy %1 %2", fileName ,sharedLocation));
            textIoFile.write(strFmt("net use %1 /delete", sharedLocation));
            textIoFile = null;
            new InteropPermission(InteropKind::ClrInterop).assert();
            process = new System.Diagnostics.Process();
            processStartInfo = new System.Diagnostics.ProcessStartInfo();
            processStartInfo.set_FileName(batchFileName);
            processStartInfo.set_WorkingDirectory(filePath);
            process.set_StartInfo(processStartInfo);
            process.Start();
            process.WaitForExit();
            process.get_ExitCode();
            CodeAccessPermission::revertAssert();
            interopPerm = new InteropPermission(InteropKind::ClrInterop);
            interopPerm.assert();
            if (System.IO.File::Exists(batchFileName))
            {
                System.IO.File::Delete(batchFileName);
            }
            if (System.IO.File::Exists(fileName))
            {
                System.IO.File::Delete(fileName);
            }
            CodeAccessPermission::revertAssert();
        }
        else
        {
           throw error("File does not exist");
        }
    }
    catch (Exception::CLRError)
    {
        netException = CLRInterop::getLastException();
        throw error(netException.ToString());
    }
}

No comments:

Post a Comment