Adding file of the same name into the same list and unable to update name of the SPFile
- by BeraCim
Hi all:
I'm having difficulties adding file of the same name in the same list and subsequently updating/changing/modifying the name of a SPFile.
Basically, this is what I'm trying to do:
string fileName = "something"; // obtained from a loop -- loop omitted here.
SPFile file = folder.Files.Add(fileName, otherFile.OpenBinary());
The Add method will generate a runtime exception when it finds another file of the same name in the same list/folder. So I thought of changing the file name later in the process:
string newGuid = Guid.NewGuid().ToString();
SPFile file = folder.Files.Add(newGuid, otherFile.OpenBinary());
// some other processing... afterwards, rename the file
file.name = fileName;
file.Item.Update();
A few minute of googling indicated I need to either move the file around, or update the name field by using ["Name"] instead.
I was wondering are there any other better ways to get around this problem?
Thanks.