Click here to Skip to main content
15,899,635 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am trying to get the list of sub-directories of a directory from svc service, but it shows an error --

The type System.IO.FileSystemInfo cannot be deserialized as it doesn't has parameter less public constructor.

Please help me out from this situation.

Code Snippet--

[OperationContract]
private System.Collections.Generic.List<DirectoryInfo> GetSubDirectories(string directoryPath)
{
    try
    {
        DirectoryInfo dir = new DirectoryInfo(directoryPath);
        DirectoryInfo[] dirs = dir.GetDirectories();
        System.Collections.Generic.List<DirectoryInfo> childs = new System.Collections.Generic.List<DirectoryInfo>();
        foreach (DirectoryInfo dir1 in dirs)
        {
           // XmlObjectSerializer xml_serial = new XmlObjectSerializer(typeof(XML_directory));
            childs.Add(dir1);
        }

        return childs;
    }
    catch (Exception) { return null; }
    //return null;
}
Posted
Updated 8-Mar-13 6:14am
v4
Comments
sjelen 8-Mar-13 12:11pm    
Improve your question with some relevant code, so we can see what you are trying to do.

1 solution

My first suggestion would have been to try to create an inherited object from the directoryinfo class. But it is sealed. Anyway I would not recomend to you to return with a list of DirectoryInfo. I would create a DTO (Data Transfer Object). See the following:

The DTO:
C#
[DataContract]
public Class DirectoryInfoDTO
{
  [DataMember]
  public DateTime CreationTime {get ; set;}
  
  [DataMember]
  public string Name {get ; set;}

}


And then the code: // Include: System.Linq;
C#
var result = dirs.Select((a,b) => new DirectoryInfoDTO()
                  {  
                      Name = a.FullName,
                      CreationTime = a.CreationTime
                  }


the result will be a collection of DirectoryInfoDTO
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900