|
-
Apr 15th, 2010, 05:48 AM
#1
[RESOLVED] Generate image path from unique name, what to do with invalid chars?
Hey,
I'm creating a radio streaming application, which plays a live stream from various radio stations in a small 'gadget' like application.
The user can add his own custom radio stations, by supplying a Name, a StreamUrl and, optionally, an Image that represents a logo or something of that radio station.
The logo will be drawn on the main form when that station is selected.
I need to be able to save these custom stations, and I think it makes sense to use an XML file, which could then look something like this
Code:
<CustomStations>
<Station>
<Name>Radio 1</Name>
<StreamUrl>http://www.someUrl.com/stream.asx</StreamUrl>
<Image>C:\somePath\radio1Img.jpg</Image>
</Station>
<Station>
<Name>Radio 2</Name>
<StreamUrl>http://www.someUrl.com/stream2.asx</StreamUrl>
<Image>C:\somePath\radio2Img.jpg</Image>
</Station>
</CustomStations>
Before I added the support for images, this worked fine, I don't need help on that.
What I do need help on is how to store the images. I want the user to be able to select any image from his harddrive, but I don't want to force him to keep that image in the same location afterwards. So I can't rely on the path that he selected. Instead, I want to simply load the image from the path he selected, and then save it somewhere else.
I am storing the XML file in the common application data path which I think is a good place for the images too.
The problem is, how to determine a name for the images? I will always have just one image per custom radio station, so it makes sense to me to simply have one Images folder that holds all images, with a unique name.
I can see two solutions:
1. Generate a random filename for every image
2. Use the Name of the radio station as the image name
For option 1, I suppose I could just create a string of 16 random characters and call that the filename. The problem of course is that there is no guarantee that I will get a unique filename. Yes, the chances of generating the same name twice randomly are very tiny, but it's still possible, and I just don't like that.
How can I ensure that my random filename is unique? I suppose I could always drop the whole thing in a While loop that continues when the new filename already exists, but there must be a better way... Is there nothing in the framework that allows me to generate a unique random filename, in a directory of my choice (so not in the Temp directory...)
I like option 2 much better. The Name of a radio station is unique (there can't be two stations with the same name), so I don't have to worry about filenames conflicting.
The problem with this approach is that there are no restrictions on the name. There's nothing stopping the user from naming their custom station "Radio~/aij!""_.fe" or something like that, which is obviously not a valid filename...
So, I will have to do 'something' to generate a valid filename from the station name. I could simply remove all non-valid path characters from the name, but then I am no longer guaranteed a unique filename... Suppose there's two stations, one called "Radio/1" and one "Radio1", then after removing the invalid "/" character, the names will be the same and so I will generate the same image path for both....
Any ideas?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|