C# question

Question Description

I’m trying to learn for my Programming class and I’m stuck. Can you help?

What does this code do? Are there any bugs? does the code require refactoring?

static void Cache(WindowsIdentity identity, FileInfo Source, FileInfo Target, Dictionary<string, ReplaceContents> rc)


  lock (lockThis)


  List<string> children = null;

  string text = null;

  byte[] data = null;

  DateTime? Source_LastWriteTime = null;

  // Get target last-modification time

  // Note: .Exists returns false for Directories, hence the Attribute checking

  //  .Attributes returns -1 on error (such as doesnt exist). Perhaps I should simply check this value? (would need further testing).

  bool Target_Exists = Target.Exists || (((int)Target.Attributes != -1) && ((Target.Attributes & FileAttributes.Directory) == FileAttributes.Directory));

  DateTime? Target_LastWriteTime = null;

  if (Target_Exists)

   Target_LastWriteTime = Target.LastWriteTime;

  // Read/Enumerate Source using impersonation

  using (identity.Impersonate())




  if (((int)Source.Attributes != -1) && ((Source.Attributes & FileAttributes.Directory) == FileAttributes.Directory))


  children = new List<string>();

  foreach (var child in Directory.EnumerateFileSystemEntries(Source.FullName))

  children.Add(new FileInfo(child).Name);


  else if (Source.Exists && ((Target_LastWriteTime == null) || (Target_LastWriteTime.Value < Source.LastWriteTime)))


  Source_LastWriteTime = Source.LastWriteTime;

  if (rc.ContainsKey(Source.Extension))

  text = File.ReadAllText(Source.FullName);


  data = File.ReadAllBytes(Source.FullName);



  catch (Exception ex)


  // Only swallow error if target already exists (assume cache has current version of file/folder)

  if (Target_Exists)






  // Source is a Directory?

  if (children != null)


  if (Target.Exists)


  else if (((int)Target.Attributes == -1) || ((Target.Attributes & FileAttributes.Directory) != FileAttributes.Directory))


  foreach (var child in children)

  Cache(identity, new FileInfo(Path.Combine(Source.FullName, child)), new FileInfo(Path.Combine(Target.FullName, child)), rc);


  // Source is a text file with a matching delegate for replacing its contents

  else if (text != null)



   text = rc[Source.Extension](Source.FullName, Target.FullName, text);

  File.WriteAllText(Target.FullName, text);

  Target.LastWriteTime = Source_LastWriteTime.Value;


  // Source is a file

  else if (data != null)



  File.WriteAllBytes(Target.FullName, data);

  Target.LastWriteTime = Source_LastWriteTime.Value;




Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors