logo

Accessing Files in a Network Share

This article features an example of accessing files in a network share. In order to receive files, you have to connect using System.IO;

//specify the path to the network share
string folderPath = @"\\10.0.0.5\Shared\Folder";
//load data about the network share
DirectoryInfo folderInfo = new DirectoryInfo (folderPath);
if (folderInfo.Exists) {
//get files with the required extension, for example .xml
var files = System.IO.Directory.GetFiles (folderPath, "*.xml", System.IO.SearchOption.TopDirectoryOnly);
//if the network share contain .xml files
if (files.Any ()) {
//get the path to the first file
var filePath = files.First ();
//create BinaryFile
var binaryFile = new BinaryFile ();
binaryFile.ContentFilePath = filePath;
binaryFile.CreateDate = DateTime.Now;
binaryFile.Name = System.IO.Path.GetFileName (filePath);
Locator.GetServiceNotNull<IFileManager> ().SaveFile (binaryFile);
}}

If you need to delete a file from the network share, use the following method:

System.IO.File.Delete (filePath);

Remember that creating BinaryFile from a file on filePath and deleting a file on filePath path you must use different transactions.