-1

I have if condition issues when running the following code for an image file:

    Dim mainFolder As String = "\\Users\No_Image_Available.png"
    Dim saveDirectory As String = "\\IMAGE\"
    Dim Filename As String = System.IO.Path.GetFileName(mainFolder)
    Dim mainSavePath As String = System.IO.Path.Combine(saveDirectory, Filename)

    If System.IO.File.Exists(mainSavePath) Then
        'do nothing
    Else
        'file doesn't exist
        My.Computer.FileSystem.CopyFile("\\Users\No_Image_Available.png", "\\IMAGES\No_Image_Available.png")

    End If

If the file doesn't exist then it will accept the if statements by either IF Not exist or IF exist... however, if the file already exist it will take the copy argument whether it is in the right if condition or not.

Why is that? It is as if it still reads and accepts the first 'do nothing' condition regardless.

FYI- the paths you see are fake

narue1992
  • 1,143
  • 1
  • 14
  • 40
  • the problem is less like with `File.Exists()` than with the path/file name passed to it. `the paths you see are fake` provide real code for real answers – Ňɏssa Pøngjǣrdenlarp Jul 15 '15 at 13:59
  • When referencing stackoverflow, someone got .Exist to work for them which is why I used it. That is the only one I know of so far to use. @Plutonix – narue1992 Jul 15 '15 at 14:00
  • Are you certain that the path to the file really exists. If you debug and copy `mainSavePath` and paste it in Explorer does the file really exist. It is possible your path may be off. What you are describing sounds exactly like the path is wrong. – logixologist Jul 15 '15 at 14:00
  • @logixologist yes I debugged the real paths and they were correct and for Plutonix's comment: the paths I use are confidential(security reasons as to why I can't provide) – narue1992 Jul 15 '15 at 14:01
  • the image is created in path. It is just when the image is already there that it still tries to copy(which ultimately flags an error that image already exist) – narue1992 Jul 15 '15 at 14:02
  • 2
    `\\IMAGE\\\Users\No_Image_Available.png` how is that a valid path? Unless you made a typo when you tried to fake the path names. – logixologist Jul 15 '15 at 14:04
  • all I am saying is double check the real path you are probably missing something. – logixologist Jul 15 '15 at 14:05
  • @logixologist my real path is like `\\web-dav\Dev\web\WebUsers\Users\No_Image_Available.png"` and other one for `saveDirectory` is like `"\\web-dav\Dev\web\WebUsers\Ads\Classified_Ads_V2\IMAGE\"` – narue1992 Jul 15 '15 at 14:06
  • @logixologist also.. it tells me the file already exist in the location so it is reading the paths. So the .Exist is my issue – narue1992 Jul 15 '15 at 14:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83338/discussion-between-logixologist-and-narue1992). – logixologist Jul 15 '15 at 14:13
  • 2
    From [MSDN](https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx):`This method also returns false if path is Nothing, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.` – Ňɏssa Pøngjǣrdenlarp Jul 15 '15 at 14:16
  • I just tested `System.IO.File.Exists(mainsavePath)` and it always throws `FALSE`. I have permissions in folder too so that can't be the issue :/ and I tested paths by adding them to windows explorer and they work. @Plutonix – narue1992 Jul 15 '15 at 14:19
  • not sure if it is because it is a server drive? before I use to use `Server.MapPath(~...)` for this path @Plutonix – narue1992 Jul 15 '15 at 14:21
  • can you try mapping a drive and trying a direct path like :G:\\Dev\web\... – logixologist Jul 15 '15 at 14:22
  • 1
    possible duplicate of [Determining if file exists using c# and resolving UNC path](http://stackoverflow.com/questions/458363/determining-if-file-exists-using-c-sharp-and-resolving-unc-path) – Ňɏssa Pøngjǣrdenlarp Jul 15 '15 at 14:22

1 Answers1

1

Issue was that they were server directories. I made the corrections that worked.

Dim Filename = "No_Image_Available.png"
    Dim savePathcopy As String = Server.MapPath("~/IMAGES/")
    Dim pathToCheckCopy As String = savePathcopy + Filename

    'Dim mainSavePath As String = System.IO.Path.Combine(saveDirectory, Filename)
    If (System.IO.File.Exists(pathToCheckCopy)) Then
        ' If System.IO.File.Exists(noImg) = True Then
    Else
        'file doesn't exist
        My.Computer.FileSystem.CopyFile("\\..\Dev\web\WebUsers\...\..\Classified_Ads_V2\No_Image_Available.png", "\\...\Dev\web\WebUsers\...\..\...\IMAGES\No_Image_Available.png")
    End If
narue1992
  • 1,143
  • 1
  • 14
  • 40