PDA

Click to See Complete Forum and Search --> : Check if directory exists


Memnoch1207
Dec 4th, 2003, 11:28 AM
I am allowing people to upload files to my server. For each new user I want to create a different directory to stored their upload files. The problem is everytime the user uploads a file it is creating a new directory...So if I upload 3 files, it creates 3 directories and stores 1 file in each.

what I want it to do is...If the user uploads a file, then create a directory to stored their files in...for each file uploaded store all the uploads for that user, in that (newly created) directory.

here is my code...I just need some help with the logic of determining if the users directory already exists.

Dim Filename As String = ""

This is where I think the problem is but how do I correct it
'Declare the creation of a new directory
Dim newDir As New DirectoryInfo(Server.MapPath("EmailAttachments\\") & Guid.NewGuid.ToString)
Dim FilePath As String = ""

FilePath = Request.ServerVariables("APPL_PHYSICAL_PATH") & "\EmailAttachments\\" & newDir.Name & "\\"

'Check to see if the directory exists
If (Directory.Exists(newDir.ToString) = False) Then
'If the Directory doesn't exist, create it.
newDir.Create()
End If

'Get the filename of the attachment (uploaded file)
Filename = Path.GetFileName(attachmentFile.PostedFile.FileName)

'Save the uploaded file to the specified FilePath
'with the specified Filename
attachmentFile.PostedFile.SaveAs(FilePath & Filename)

'Add the uploaded files name to the listbox
lstAttachments.Items.Add(Filename)

pvb
Dec 4th, 2003, 06:32 PM
If you have a way of identifying users maybe come up with a way of incorporating that info into the naming of the directory, somethin like UserDirectory_PlaceTheirUserIdHere, then check if their directory exists. You could also save the guid in a database table associated with each user, then check if that entry exists before creating a new one, otherwise create a new directory and save that name off somewhere associated with the user.