Tuesday, August 28, 2012

Read / Write Files from document handling

Question:
How can you read / writes files from document handling?

Answer:

void fromFolderToDB(FilePath _path)
{
System.IO.DirectoryInfo         dir = new
System.IO.DirectoryInfo(_path);
System.IO.FileInfo[]            files;
System.IO.FileInfo              curFile;
System.Collections.IEnumerator  enumerator;
BinData                         binData;
DocuValue                       docuValue;
;
files = dir.GetFiles(_path);
enumerator = files.GetEnumerator();
while (enumerator.MoveNext())
{
curFile = enumerator.get_Current();
docuValue.clear();
binData.loadFile(curFile.get_FullName());   // get file content
docuValue.File      = binData.getData();
docuValue.FileName  = curFile.get_Name();
docuValue.insert(); // insert into DB
}
}
void fromDBToFolder(FilePath _path)
{
BinData                         binData;
DocuValue                       docuValue;
;
while select docuValue // load From DB
{
binData.setData(docuValue.File);
binData.saveFile(docuValue.FileName); // insert into file system
}
}
Make attention to CodeAccessPermission when reading/writing to file system
and when using CLR inside X++

3 comments: