If you send a DirectoryInfo instance from PC1 to PC2, is GetFiles or GetDirectories still working?
public void DirTest()
{
//On first PC:
DirectoryInfo driveC = new DirectoryInfo(@"C:\randomdir\");
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ns = new MemoryStream();
bf.Serialize(ns, driveC);
SendStream(ns); //Sending the stream to the second PC
//On second PC:
ns = ReceiveStream(); //Receiving the stream from the first PC
ns.Position = 0;
DirectoryInfo di = (DirectoryInfo)bf.Deserialize(ns);
//Does this work?
foreach (FileInfo item in di.GetFiles())
{
Debug.WriteLine(item);
}
}
If you execute that code on the same PC it works but i dont have the enviroment to test if this works on 2 different pcs.
Maybe the SubDirectories and Files are saved in an array in the directoryinfo class because i found this serialization function:
Well, it only works if you have the same directory name on both machines. Let's take a look at the reference source ...
First, DirectoryInfo
inherits FileSystemInfo
, so when you deserialize DirectoryInfo
, this constructor is called:
[System.Security.SecurityCritical] // auto-generated
private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
{
Directory.CheckPermissions(string.Empty, FullPath, checkHost: false);
DisplayPath = GetDisplayName(OriginalPath, FullPath);
}
Where base
is a FileSystemInfo
, and this constructor is used:
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
protected FileSystemInfo(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException("info");
Contract.EndContractBlock();
// Must use V1 field names here, since V1 didn't implement
// ISerializable.
FullPath = Path.GetFullPathInternal(info.GetString("FullPath"));
OriginalPath = info.GetString("OriginalPath");
// Lazily initialize the file attributes.
_dataInitialised = -1;
}
So you can see that the only thing that is serialized isis the FullPath
and OriginalPath
values. The data inside the directory is not serialized and if you call DirectoryInfo.GetFiles()
you will enumerate the files in the local computer, not the computer that serialized the DirectoryInfo
in the first place. In fact the source specifically says Lazily initialize the file attributes
, which means that they are loaded when requested.
// Returns an array of Files in the DirectoryInfo specified by path
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public FileInfo[] GetFiles()
{
return InternalGetFiles("*", SearchOption.TopDirectoryOnly);
}
Which calls:
// Returns an array of Files in the current DirectoryInfo matching the
// given search criteria (ie, "*.txt").
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
private FileInfo[] InternalGetFiles(String searchPattern, SearchOption searchOption)
{
Contract.Requires(searchPattern != null);
Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
IEnumerable<FileInfo> enble = FileSystemEnumerableFactory.CreateFileInfoIterator(FullPath, OriginalPath, searchPattern, searchOption);
List<FileInfo> fileList = new List<FileInfo>(enble);
return fileList.ToArray();
}
And again, you see that nothing is used from the serialized information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.