Detect when OpenFileDialog returns a loaded URL / URI

I am using OpenFileDialog (.Net Framework 4, Windows 10) and I noticed that it will allow the user to specify the URL as the file name (e.g. http: //somewebsite/picture.jpg ). This is very useful for my application, so I do not intend to disable it. The way it works is to upload the file to the user's temporary files directory and return the temporary file name to the Filename property property. This is good, except for the fact that the user begins to create garbage in his temp directory.

I would like to say when the file was loaded by the OpenFileDialog class (as opposed to a previously existing file), so I can clear it by deleting the file after use. I can check if the file directory is a temporary directory, but this is not very good, as the user could download the file himself.

I tried to catch the FileOK event and check the Filename property to see if it is an HTTP / FTP URI, but despite what the documentation says (“Occurs when a user selects a file name by clicking the Open Open File File Dialog button), he it starts after the file is downloaded, so I do not get access to the URL: the Filename property already has a temporary file name.

EDIT: This is an example of what I like:

Dim dlgOpenFile As New System.Windows.Forms.OpenFileDialog

If dlgOpenFile.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Return

''//do some stuff with dlgOpenFile.Filename

If dlgOpenFile.WasAWebResource Then
    Dim finfo = New IO.FileInfo(dlgOpenFile.Filename)
    finfo.Delete()
End If

dlgOpenFile "WasAWebResource", , . , .

+4
1

, , , ? , " ", , :

FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{

    string temporaryInternetFilesDir = Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
    if (!string.IsNullOrEmpty(temporaryInternetFilesDir) && 
                dialog.FileName.StartsWith(temporaryInternetFilesDir, StringComparison.InvariantCultureIgnoreCase))
    {
        // the file is in the Temporary Internet Files directory, very good chance it has been downloaded...
    }
}
+1

Source: https://habr.com/ru/post/1607211/


All Articles