i'm not exactly sure what you are asking here, so i'm making some assumptions, which are:
(a) you have a fixed number of files on your server,
(b) those files are all in the same location, i.e., directory,
(c) their filenames all have the format: nnn_aaaaaa...aaa.pdf,
(d) your end user is only passing you the nnn part (perhaps by clicking a link?),
(e) you will let your end user download the file that is associated with the
nnn part.
TIMTOWTDI =. there is more than one way to do it
from what little i think i know, based on my assumptions above, if there are not a large number of files, and the files do not change often, i might use a
Dictionary<Int32, String> which would contain entries like:
if your files are in different directories, you could store the full path in the dictionary instead of simply the filename.
if you have gazillions of files, you could store the filenames (or full paths) in a database and do a lookup against your database.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
After reading your question several times I ended up thinking in a scenario as below:
You may create a table in your globally accessible dictionary / a table in database / globally accessible in memory table (System.Data.DataTable should suffice too), that contains one to one ID <--> meaningful name's pairs. e.g. :
ID
<--->
Name
21
<--->
Grocery
81
<--->
Medicines
108
<--->
Softwares
118
<--->
Dairy Products
And can use while buidling the file names, like "21Grocery.PDF" ( Or "21_Grocery.PDF" ) etc.
I hope this helps you (if I could have understood your question properly) and if not please post ...
banksidepoet
Participant
774 Points
862 Posts
Get the fisrt few characters of a string up to a marker
Dec 08, 2012 09:59 AM|LINK
I have a scenario in which I have a dynamic page into which is passed an ID value.
I use that value to create a download link for a PDF file that has the filename <ID>.pdf.
I would rather use more descriptive filenames for the PDF's such as <ID>_technical.pdf
(Just to be clear, instead of 23.pdf, I want 23_technical.pdf).
****
I can't use the filename in the string which identifies what PDF to download so I am faced with needing some C# which (pseudo code) says:
"download the PDF with filename that STARTS with <ID>.
I need to just grab the few characters up to - but not including - the underscore.
The <ID> may be two or three characters long.
Is this possible?
Thanks.
MetalAsp.Net
All-Star
112231 Points
18268 Posts
Moderator
Re: Get the fisrt few characters of a string up to a marker
Dec 08, 2012 01:59 PM|LINK
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Get the fisrt few characters of a string up to a marker
Dec 08, 2012 02:29 PM|LINK
@ banksidepoet
i'm not exactly sure what you are asking here, so i'm making some assumptions, which are:
(a) you have a fixed number of files on your server,
(b) those files are all in the same location, i.e., directory,
(c) their filenames all have the format: nnn_aaaaaa...aaa.pdf,
(d) your end user is only passing you the nnn part (perhaps by clicking a link?),
(e) you will let your end user download the file that is associated with the nnn part.
TIMTOWTDI =. there is more than one way to do it
from what little i think i know, based on my assumptions above, if there are not a large number of files, and the files do not change often, i might use a Dictionary<Int32, String> which would contain entries like:
16 16_aaaabbbbcccc.pdf
23 23_technical.pdf
65 65_xxxxyyyyzzzz.pdf
Then you simply look up your document.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx "Dictionary<TKey, TValue> Class"
if your files are in different directories, you could store the full path in the dictionary instead of simply the filename.
if you have gazillions of files, you could store the filenames (or full paths) in a database and do a lookup against your database.
g.
Paul Linton
Star
13431 Points
2535 Posts
Re: Get the fisrt few characters of a string up to a marker
Dec 08, 2012 08:59 PM|LINK
First get a DirectoryInfo object for the directory where the files are stored then use the GetFiles method to find your file. Something like
var pdfHomeDir = new DirectoryInfo(Server.MapPath("/MywebSite/pdfFolder")); FileInfo desiredPDF = pdfHomeDir.GetFiles(theCharactersThatYouKnow + "_*.pdf").FirstOrDefault(); string desiredPDFFIleName = desiredPDF.FullName;aarsh
Participant
1543 Points
428 Posts
Re: Get the fisrt few characters of a string up to a marker
Dec 10, 2012 02:10 AM|LINK
After reading your question several times I ended up thinking in a scenario as below:
I hope this helps you (if I could have understood your question properly) and if not please post ...
Thanks,
-Aarsh