Anand
Anand

Reputation: 11

C# connect to IIS Server and compare Server Website folder and Client Folder on local Machine

How to connect server already hosted on IIS Website Folder and compare the Local Client Folder?

This is my Website URL on localhost

 IISHostedWebsite/Updates         //Folder in Website

I need to compare in this url update folder files with my local client machine's D:\Updates folder.

If new updates available into server it will copy to my D:\Updates folder.

How can we achieve this sort of situation ?

I have some code that in C#

 var directory = new DirectoryInfo(@"D:\\Anand\\Work\\FolderCheck\\Server");
 var myFile = (from f in directory.GetFiles()
               orderby f.LastWriteTime descending
               select f).First();

This code generates the latest updated file from folder

Upvotes: 0

Views: 428

Answers (2)

Anand
Anand

Reputation: 11

Here my code is for Getting Files from Directory and Check Whether are same if not It will Showing No updates Found and After that It will Check latest Files Available into Folder if Available then Copy this Files and Move to Destination Folder This is my Task but i have completed my code in Latest updated file i get from this code but i dont no how this files are copy into destination folder

 string serverPath = @"D:\Anand\Work\FolderCheck\Server";    //Source File
 string clientPath = @"D:\Anand\Work\FolderCheck\Client";   //destination Folder



  private static bool CompareFileSizes(string fileServer, string fileClient)
    {
        bool fileSizeEqual = true;

        if (fileServer.Length == fileClient.Length)  // Compare file sizes
        {
            fileSizeEqual = false;        // File sizes are not equal therefore files are not identical
        }
        return fileSizeEqual;
    }

 try
        {
            if (!File.Exists(serverPath) || !File.Exists(clientPath))
            {
                try
                {
                    var Server = Path.GetFileName(serverPath);
                    var Client = Path.GetFileName(clientPath);

                    string ServerFile = Server.ToString();
                    string ClientFile = Client.ToString();

                    if (CompareFileSizes(ServerFile, ClientFile))
                    {
                        lblServerMsg.Text = "No Updates are Found: ";
                    }
                    else
                    {
                        var directoryServer = new DirectoryInfo(@"D:\Anand\Work\FolderCheck\Server"); //check latest Available File From Server                          
                        var myFile = (from f in directoryServer.GetFiles()
                                      orderby f.LastWriteTime descending
                                      select f).First();


                        lblServerMsg.Text = "Updates Are Available Click for Update Button:";
                        btnCheckUpates.Visible = false;
                        btnUpdates.Visible = true;
                    }

                }
                catch (Exception msg)
                {
                    lblServerMsg.Text = "No Updates are Found: " + msg.Message;
                }
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException ex)
        {
            lblServerMsg.Text = ex.Message;
        }

Above code i have done Get latest File from Source Folder but i dont no how to

var myFile = (from f in directoryServer.GetFiles()
                                      orderby f.LastWriteTime descending
                                      select f).First();

Above doe myFile are latest File from Source Folder this i want to copy into Destination Folder

Upvotes: 0

BilalAlam
BilalAlam

Reputation: 1227

The MSDeploy tool (http://www.iis.net/downloads/microsoft/web-deploy) was designed to solve this problem. It lets you compare IIS virtual directories to other directories, and synchronize them if needed. It can also be used just as a diff tool.

In your example, after installing MSDeploy, you could do the following:

msdeploy.exe -verb:sync -source:contentPath="IISHostedWebSite/Updates" -dest:contentPath=d:\updates -whatIf

This command will show the list of changes needed to update d:\updates to look like IISHostedWebSites/Updates. If you remove the "-whatif" it will actually do the changes.

You can also call the MSDeploy programatically to do the same thing.

Also the problem with your code snippet is that you wouldn't detect deleted files from source that also need to be deleted from destination.

Upvotes: 2

Related Questions