473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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.
Oct 27 '09 #1
9 1947
tlhintoq
3,525 Expert 2GB
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.
Oct 27 '09 #2
cloud255
427 Expert 256MB
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.
Oct 27 '09 #3
tlhintoq
3,525 Expert 2GB
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
Oct 27 '09 #4
cloud255
427 Expert 256MB
@tlhintoq
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...
Oct 27 '09 #5
tlhintoq
3,525 Expert 2GB
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?
Oct 27 '09 #6
cloud255
427 Expert 256MB
@tlhintoq
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 :)
Oct 28 '09 #7
MrMancunian
569 Expert 512MB
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
Oct 28 '09 #8
tlhintoq
3,525 Expert 2GB
@MrMancunian
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?
Oct 28 '09 #9
cloud255
427 Expert 256MB
@tlhintoq
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...
Oct 28 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
6
VijaySofist
by: VijaySofist | last post by:
HI Guys! Visual Studio 2008 has been recently installed in my system. It is not working in my system. The problems that i am facing are as follows: When opening a New Project, It gives the...
2
by: Nikolay Belyh | last post by:
1. Why does Visual Studio 2008 started ignoring //{NO_DEPENDENCIES} in resource.h? If I edit file "resource.h" _manually_, it rebuilds the whole solution. It worked just fine with Visual Studio...
4
by: lichaoir | last post by:
Can I develop asp.net 1.1 application in visual studio 2008? I've tried to find .net framework 1.1 in the project's properties window, but I can't find it! Please help! Thanks for your...
2
by: Cramer | last post by:
So, what is the relationship between Visual Studio and Visual Web Developer. I find a lot of documentation on MSDN that presents Visual Web Developer as it's own stand-alone product (which I'd...
6
by: Miro | last post by:
I can run an exe ( and its install ) i have created on my machine. The exe has a button that populates a dataset and then shoots it to a crystal report. But... Installing the setup.exe on my...
4
by: Miro | last post by:
<i have also added this reply to the other newsgroup - now that I have realizd ( and assuming ) it is not a localized error directly to vb.> I have found this link on the website:...
5
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I am trying to get the DNS name of an arbitrary IP address on the network. If I use GetHostEntry as the documentation suggests I only get the name of the machine I am running the code on. All...
0
by: _Who | last post by:
I'm trying to free up some space on my system disk. In Add or Remove Programs I see: Microsoft Windows SDK for Visual Studio 2008 .NET Framework Tools Microsoft Windows SDK for Visual Studio...
3
by: Johnson | last post by:
I'm not sure if this is an IIS 5.1 issue or ASP.NET issue, or Visual Studio 2008 issue -- thus posting to 3 groups. Please don't be offended. The problem I'm encountering is that Visual Studio...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.