Sebelumnya, pastikan sudah dapat hak akses untuk bisa hapus folder dan file tersebut
static void Test_Move_One_File(Args _args)
{
System.Exception ex;
Set permSet = new Set(Types::Class);
str errorMsg;
FileName _srcFile,_destFile;
;
_srcFile = "D:\\Move_Src\\File.txt";
_destFile = "D:\\Move_Dst\\File.txt";
try
{
permSet.add(new FileIOPermission(_srcFile, 'rw'));
permSet.add(new InteropPermission(InteropKind::ClrInterop));
permSet.add(new FileIOPermission(_destFile, 'rw'));
CodeAccessPermission::assertMultiple(permSet);
//Kalo file sumber tidak ada, maka batalkan proses
if(!System.IO.File::Exists(_srcFile))
{
Info("Source file not exist !! Proces terminated.");
Break;
}
else
{
Info("Source file exist !! File will be moved.");
if(System.IO.File::Exists(_destFile))
{
System.IO.File::Delete(_destFile);
}
}
System.IO.File::Move(strfmt(@"%1",_srcFile), strfmt(@"%1",_destFile));
CodeAccessPermission::revertAssert();
}
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
error(ex.ToString());
}
}
}
}
No comments:
Post a Comment