|
-
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?
-
Apr 15th, 2010, 06:21 AM
#2
Frenzied Member
Re: Generate image path from unique name, what to do with invalid chars?
you can asign an unique id to the radio station and use that id as the image name
-
Apr 15th, 2010, 06:27 AM
#3
Re: Generate image path from unique name, what to do with invalid chars?
You have this kind of options. You can go for the
Code:
Path.GetTempFileName()
Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.
Code:
System.Guid.NewGuid
to get a unique GUID
Or You can use the current time(with seconds) with that RadioButton to create unique id
Code:
System.DateTime.Now.ToString("ddMMyyyhhmmss")
Please mark you thread resolved using the Thread Tools as shown
-
Apr 15th, 2010, 06:33 AM
#4
Re: Generate image path from unique name, what to do with invalid chars?
 Originally Posted by aashish_9601
you can asign an unique id to the radio station and use that id as the image name
I thought of that but that is not an option, for other reasons. Perhaps as a last resort.
 Originally Posted by danasegarane
You have this kind of options. You can go for the
Code:
Path.GetTempFileName()
Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.
Code:
System.Guid.NewGuid
to get a unique GUID
Or You can use the current time(with seconds) with that RadioButton to create unique id
Code:
System.DateTime.Now.ToString("ddMMyyyhhmmss")
The GetTempFileName creates a filename in the Temp folder, right? I don't want that. I suppose I could always use only the last part of the path (the actual filename), but that would leave me with the same problem I think. I suppose the GetTempFileName generates a unique name in the Temp folder, which doesn't have to be unique in my own folder. So if I take this route I may as well just generate my own random name and hope I never get a conflict.
I'll look into the Guid thing, never used that before.
The time is not an option, because the custom stations are all saved in one go, along with their images, so the images would all have the same timestamp.
-
Apr 15th, 2010, 06:37 AM
#5
Re: Generate image path from unique name, what to do with invalid chars?
 Originally Posted by NickThissen
I thought of that but that is not an option, for other reasons. Perhaps as a last resort.
The time is not an option, because the custom stations are all saved in one go, along with their images, so the images would all have the same timestamp.
But the Seconds will differ.Isn't it ?
Please mark you thread resolved using the Thread Tools as shown
-
Apr 15th, 2010, 06:45 AM
#6
Re: Generate image path from unique name, what to do with invalid chars?
Maybe the milliseconds, definitely not the seconds. And I heard the system clock isn't very accurate into the milliseconds so I'm afraid the timestamp could be exactly the same for two or more images.
-
Apr 15th, 2010, 06:53 AM
#7
Addicted Member
Re: Generate image path from unique name, what to do with invalid chars?
...
vb Code:
Dim YourString As String = GenerateRandomString() While StringValid(YourString) = False YourString = GenerateRandomString() End While Function StringValid(String S) As Boolean If ImageFolder Contains File Named S Then Return False Return True End Function
-
Apr 15th, 2010, 11:29 AM
#8
Re: Generate image path from unique name, what to do with invalid chars?
 Originally Posted by NickThissen
The GetTempFileName creates a filename in the Temp folder, right? I don't want that. I suppose I could always use only the last part of the path (the actual filename), but that would leave me with the same problem I think. I suppose the GetTempFileName generates a unique name in the Temp folder, which doesn't have to be unique in my own folder. So if I take this route I may as well just generate my own random name and hope I never get a conflict.
http://msdn.microsoft.com/library/aa364991
"Only the lower 16 bits of the uUnique parameter are used. This limits GetTempFileName to a maximum of 65,535 unique file names if the lpPathName and lpPrefixString parameters remain the same.
Due to the algorithm used to generate file names, GetTempFileName can perform poorly when creating a large number of files with the same prefix. In such cases, it is recommended that you construct unique file names based on GUIDs."
 Originally Posted by NickThissen
I'll look into the Guid thing, never used that before.
This is a very straightforward process.
Code:
Dim uniqueFile As String = String.Format("Img{0}.jpg", Guid.NewGuid.ToString())
-
Apr 15th, 2010, 11:55 AM
#9
Re: Generate image path from unique name, what to do with invalid chars?
Thanks guys, the Guid method works well.
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
|