SimonSimCity / Xamarin-CrossDownloadManager

A cross platform download manager for Xamarin
MIT License
149 stars 68 forks source link

Check if file exists #111

Closed lepetitpaco closed 5 years ago

lepetitpaco commented 5 years ago

I have issues checking if file exists, in main activity my code is

CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<IDownloadFile, string>
                (file =>
                {
                    string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
                    string fullpath = Path.Combine(ApplicationContext.GetExternalFilesDir
                        (Android.OS.Environment.DirectoryMovies).AbsolutePath, fileName);
                    if (File.Exists(fullpath))
                    {
                        return null;
                    } else {
                    return fullpath;
                    }
                });

If the file doesn't exist, the file is going to download where I want it to be, so alright on that part.

If the file exists, it is going to download my file but it's going to be named like file-15.mp4 or file-30.mp4, always a random number. And I don't want it to download if file does exist.

Thanks for any help you might be able to give.

SimonSimCity commented 5 years ago

The method you're using there should always return a string for the new filename. If you want to check if the file already exists, you should do this before starting the download.

Depending on the platform, the method you assign to PathNameForDownloadedFile is either called before or after the download actually starts, but in both cases it will overwrite a file already existing at the given path.

SimonSimCity commented 5 years ago

@lepetitpaco if this answered your question, please close the issue. I'll close it after 30 days without response.