views:

57

answers:

1

I hope the title is not too confusing. I am trying to make folders with linq-to-sql objects' IDs. Actually I have to create folders before I should save them. I will use them to keep user uploaded files. As you can see I have to create the folder with the FileID before I can save it there. So I just save a record which will be edited or maybe deleted

File newFile = new File();
...//add some values to fields so they don't throw rule violations
db.AddFile(newFile);
db.Save();
System.IO.Directory.CreateDirectory("..Uploads/"+newFile.FileId.ToString());

After that I will have to edit some fields and save again. Of course user might stop upload and I would have to delete it. I know I can write a stored procedure to get the next available FileID but some other upload happening at the same time would get the same number. So they would write in same directory which is a thing I don't want. Should I go on with this, would there be some problems? Can you think of a better way?

+3  A: 

Well.. one thing you could do is generate a GUID, and use that for your folder name instead

string uniqueDirectory = System.Guid.NewGuid().ToString("N");
System.IO.Directory.CreateDirectory("..Uploads/"+uniqueDirectory );
//handle upload, save file, errors,  etc..
...
//now that the file is uploaded save to the database
File newFile = new File();
newFile.FolderName=uniqueDirectory;
db.AddFile(newFile);
db.Save();
markt
Thank you. Shame on me for not stumbling upon GUIDs before.
Ufuk Hacıoğulları