0

In my Console App (c#) project, I'm trying to let the user type a list of one or more network locations, on a secure network that doesn't have outside connectivity. The user will have full admin rights. The user provides: Source: "machine name" and Destination: "network location".

It's supposed to run against XP and Win7 machines and get the "Documents", "Desktop", "Favorites" and "Application Data".

It works on XP for "Documents", "Desktop" and "Favorites", but not the Hidden "Application Data", and on Win7 it works for everything except "Documents".

Every post and link I've researched says this is a huge security risk and it can't be done.

Is there a library that can handle this? If not, can someone confirm that "It can't be done."?

I've tried Directory, DirectoryInfo, FileAttributes, and WMI.

Here is my reduced code:

    if (Str_Drive.Length == 0)
    {
        str_SrcAndDrive = @"\\" + str_Source.ToString() + @"\c$";
    }
    else
    {
        str_SrcAndDrive = @"\\" + str_Source.ToString() + @"\" + Str_Drive + "$";
    }

    //XP:
    string str_basePath = Path.Combine(str_SrcAndDrive, "Documents and Settings");
    var dir_base = new DirectoryInfo(str_basePath);
    //System.OperatingSystem osInfo = System.Environment.OSVersion;
    if (dir_base.Exists) //&& (osInfo.Platform == System.PlatformID.Win32NT)) //XP
    {
        foreach (var dir in dir_base.GetFileSystemInfos())
        {
            switch (dir.ToString())
            {
                 case "administrator":
                 case "Administrator":
                 case "Default User":
                 case "All Users":
                 //Do nothing
                      break;
                 default:
                      string str_dir = dir.ToString();

                      //Code to show hidden directories
                      DirectoryInfo di = new DirectoryInfo(Path.Combine(str_basePath, str_dir));
                      string dirPath = "";
                      DirectoryInfo[] diArr = di.GetDirectories();
                      foreach (DirectoryInfo dri in diArr)
                      {
                           dirPath = dri.ToString();
                           FileAttributes attributes = File.GetAttributes(dirPath); //blows up here
                           if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                           {
                               File.SetAttributes(dirPath, File.GetAttributes(dirPath) & ~FileAttributes.Hidden);
                               File.SetAttributes(dirPath, attributes);
                           }
                      }
                      //Handle XP App Data
                      string str_baseAndDirsPath = Path.Combine(dir_base.ToString(), str_dir, "Application Data");
                      DirectoryInfo dir_baseAndDirs = new DirectoryInfo(str_baseAndDirsPath);
                      if (dir_baseAndDirs.Exists)
                      {
                          string str_Destination = Path.Combine(str_Dest, str_Source, str_dir, "Application Data");
                          ProcessXcopy(str_baseAndDirsPath, str_Destination);
                      }  //continues, but this gives you the idea...
BradAtCC
  • 41
  • 1
  • 10
  • Have you tried the [Environment.SpecialFolder Enumeration](http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx). And specifically, the `ApplicationData` member? – Metro Smurf Dec 19 '13 at 20:03
  • I believe so. The Environment Enumerations only work with the current computer, correct? I'm trying to work with Other remote Workstations on the network. – BradAtCC Dec 19 '13 at 20:06
  • When you do `Directory.GetDirectories()`, on the User's Documents and Settings folder, does it list out the folders you have been unable to copy? What you're trying to do should be possible, assuming the connecting user has authorization. You may need to look at this answer for connecting with credentials. http://stackoverflow.com/questions/295538/how-to-provide-user-name-and-password-when-connecting-to-a-network-share – Pete Garafano Dec 19 '13 at 20:06
  • It does list out all the directories, including the "Application Data" folder, but when I attempt to pass the path: "\\Workstation\\Document and Settings\\Application Data" it blows up where that's commented. And when the code reached the if (dir_baseAndDirs.Exists) check, it doesn't see the "Application Data" as existing. – BradAtCC Dec 19 '13 at 20:10
  • The FileAttributes bit, is the latest add in an attempt to change the Hidden status of that folder. – BradAtCC Dec 19 '13 at 20:11
  • 1
    You need to impersonate an account that has access to those paths. – josh poley Dec 19 '13 at 20:16
  • All the potential users of this tool, are full administrators, with full access. But, let me look into this some...would that be as easy as (do impersonate code) using received inside the "Document and Settings" folder? – BradAtCC Dec 19 '13 at 20:39

0 Answers0