Copying Files from a remote workstation to a network location in a c# Console App

145 views Asked by At

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...
0

There are 0 answers