Connecting Tech Pros Worldwide Help | Site Map

i am working in C# with visual studio 2008. can any one help me please ASAP

Newbie
 
Join Date: Feb 2007
Posts: 2
#1: 3 Weeks Ago
i want to save a file/image into folder on the server with an unique filename so, that files with same should not overwrite.

ican store the file/image into file but not with the unique file name ...

ex:- if i got an image file as

jhony.jpg while saving it should save as 1234567J_Jhony.jpg into the folder on a button click

and at the same click the name of the file should go into the database.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


You can check if a file name already exists:
Expand|Select|Wrap|Line Numbers
  1. bool  AlreadyExists = System.IO.File.Exists(szFileName);
If it does exist, give it a new name.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#3: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


I normally append a timestamp to the original file name to create unique file names. Something like:

Expand|Select|Wrap|Line Numbers
  1. string newFileName = oldFileName + DateTime.Now.ToString();
This will almost always provide a unique filename, if it fails you can always use the DateTime.Now.Ticks.ToString(), to ensure a unique file name.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#4: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Long ago I created a function called "UniqueName(string FilePath)" that mimic the Windows scheme of:
MyFile.jpg
MyFile(1).jpg
MyFile(2).jpg

It's little more than an incrementing loop:
Does file exist "MyFile(2).jpg"
Yes: increase the suffix. Check again
No: Write a zero k file here to reserve the new name. Return the new name
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#5: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:

Originally Posted by tlhintoq View Post

Long ago I created a function called "UniqueName(string FilePath)" that mimic the Windows scheme of:
MyFile.jpg
MyFile(1).jpg
MyFile(2).jpg

It's little more than an incrementing loop:
Does file exist "MyFile(2).jpg"
Yes: increase the suffix. Check again
No: Write a zero k file here to reserve the new name. Return the new name

Hmmm, how did your execution time compare to the standard Windows version of this functionality? It sure would make my file names more user friendly...
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#6: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:

Originally Posted by cloud255

Hmmm, how did your execution time compare to the standard Windows version of this functionality? It sure would make my file names more user friendly...

How does one test the actual speed of how the OS does this? For that matter, I don't know a way to ask the OS for the next available name. So I can only go by how it feels, and it feels fast. Well, as fast Windoze can be expected I guess.

All of my file save functions use the "UniqueFileName" method. More or less to this effect:

Expand|Select|Wrap|Line Numbers
  1. tlhintoq.GDI.Graphics.Save(Image ImageObjectToSave, String FilePath)
  2. {
  3.       if (MakeDirForFIle(FilePath)) /* return true if folder exists, making if needed */
  4.       {
  5.       string NewFilePath = UniqeFilename(FilePath);//This reserves the name
  6.       switch (System.IO.Path.GetExtension(FilePath).ToLower().TrimStart('.'))
  7.    {
  8.       case "jpg":
  9.          SaveJPG(ImageObjectToSave, NewFilePath);
  10.          break;
  11.       case "bmp"
  12.          // Call bitmap save function
  13.          break;
  14.       case "tga"
  15.          // Call Targa save function
  16.          break;
  17.       }
  18.    }
  19. }
And they seem to be working quite fast. Lets face it, most times there isn't an existing file. How many times is the loop going to execute MOST times? 3 maybe?
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#7: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:

Originally Posted by tlhintoq View Post

And they seem to be working quite fast. Lets face it, most times there isn't an existing file. How many times is the loop going to execute MOST times? 3 maybe?

I really like this approach, I'll play around with it on our systems (we often have dozens of files with the same name) and see if the performance hit is not too bad.

I suppose that I could create some sort of dictionary which will store the 'base' file names ('myImage') and also then the last generated name ('myImage(3)'), which leaves me needing to do only a lookup, string manipulation and insert instead of the loop. But that might be an overkill just to generate unique file names :)
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#8: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:
and at the same click the name of the file should go into the database.
If you are going to put it in the database, why don't you return the primary key for that record (which should be unique) and use that as a prefix for your file? That way, you don't have to check if the file already exists.

Steven
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#9: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:

Originally Posted by MrMancunian View Post

If you are going to put it in the database, why don't you return the primary key for that record (which should be unique) and use that as a prefix for your file? That way, you don't have to check if the file already exists.

Steven

If you write 5 files, all to the same database, wouldn't they all have the same primary key and thus still be returned with the same name?
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#10: 3 Weeks Ago

re: i am working in C# with visual studio 2008. can any one help me please ASAP


Quote:

Originally Posted by tlhintoq View Post

If you write 5 files, all to the same database, wouldn't they all have the same primary key and thus still be returned with the same name?

No, primary key is a unique identifier per record (row) in a table. So appending the primary key, or using it as a file name will create a unique name for the entire application. But for this to work you need to design a table specifically for this purpose...
Reply