Izzy
Izzy

Reputation: 6876

Checking to see if file exist after it is downloaded from server

I'm fairly new to C# programming so do excuse me if I'm not putting this across correctly.

I am writing a C# app which uses client/server communication via TCP/IP. I pass certain parameters through to get a file from the server which is running on another machine (client and server are both on same network).

I can manage to get the file without a problem. However, once I have received the file I want the client to check in the directory, and send a message back to the server if the file exist. If it does not, run the method which gets the file.

void CheckFile() 
{
    string fileLocation = "C:\";
    if (File.Exists(fileLocation))
    {
        Console.WriteLine("File Exist");
    }
    else
    {
        // What do i put here?
    }
}

void Main() 
{
   string fname = "/test.txt"
  //Creating streamwriter object to send messages to server
    StreamWriter writer = new StreamWriter(serverstream);
    writer.AutoFlush = true;
    writer.WriteLine(fname);//Sends the file name to the server
}

In the main method I use StreamWriter to send messages across to server. Main Method also gets the file from the server.

Upvotes: 1

Views: 1664

Answers (1)

Pseudonym
Pseudonym

Reputation: 2072

Summary from MSDN on File Exist method:

The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Exists returns false.

To check whether the path contains any invalid characters, you can call the GetInvalidPathChars method to retrieve the characters that are invalid for the file system. You can also create a regular expression to test the whether the path is valid for your environment.

For examples of acceptable paths, see File. Be aware that another process can potentially do something with the file in between the time you call the Exists method and perform another operation on the file, such as Delete. The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory. If path describes a directory, this method returns false. Trailing spaces are removed from the path parameter before determining if the file exists.

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Sample code also from MSDN:

string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");

Link where I got all this info:

http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

UPDATE

Based on your comments you are looking to first check if the file exists which my original answer will solve.

Second you want to verify what you downloaded is the same as what is on the server. For that the easiest method is to compute a hash of the file and compare it against a the hash of the file on the server if they are equal then the files are exact copies of each other, if not then something went wrong in the download.

Check out this MSDN link for MD5 hashing

http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5(v=vs.110).aspx

Their example is pretty complex but thorough, so look it over and then maybe do some searching for a more simple implementation.

Related SO question: Calculate MD5 checksum for a file

Upvotes: 1

Related Questions