Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Could not find a part of the path while doing a File Copy

3.89/5 (4 votes)
2 Mar 2012CPOL 27.6K  
If at all possible, never give your code the opportunity to fail. If there's any possibility at all that you can prevent an error condition from being raised, do it. try{ if (!string.IsNullOrEmpty(FileName)) { string newPath = System.IO.Path.Combine(FilePath,...
If at all possible, never give your code the opportunity to fail. If there's any possibility at all that you can prevent an error condition from being raised, do it.

C#
try
{
    if (!string.IsNullOrEmpty(FileName))
    {
        string newPath = System.IO.Path.Combine(FilePath, FileName); 
        if (File.Exists(Path) && Path.ToLower() != newPath.ToLower())
        {
            File.Copy(Path, newPath, true);
        }
    }
}
catch (Exception ex)
{
    // handle the exception as desired
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)