Build SPFile Url’s Into A String Array
There was a question that was posted in the SharePoint development newsgroups as to how one would go about building the URL properties of SPFile objects into a string array. Here is a solution that seems to work just fine:
- SPFolder Folder;
- int counter = Folder.Files.Count;
- string array = new string[counter + 1];
- int index = 1;
- foreach (SPFile file in Folder.Files)
- array.SetValue(file.Url, index);
You can see that we are firstly hydrating an SPFolder object representing the location where the holder of the relevant files are going to be looped through. Secondly, we declare the counter field that is going to take the SPFolder object and start a count on the files so that we can gauge the segments that are going to be built into the string array, incremented by uping the counter by a numeric of 1. Following, we are going to declare a index integer that is going to iterate through the SPFile objects that are in SPFolder.Files, then take the SPFile.URL property and add it to the string array. The result of which you could use for whatever. Obviously the SPFile.Url property could be adjusted to whatever SPFile property that you wanted, it wouldn’t have to be that. You could use it for doing other things like building an array of SPFile property metadata, or the likes if you wanted as well, pretty much whatever you want.
Hope this helps someone else.