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.
9 1947
You can check if a file name already exists: - bool AlreadyExists = System.IO.File.Exists(szFileName);
If it does exist, give it a new name.
I normally append a timestamp to the original file name to create unique file names. Something like: - 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.
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
@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...
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: - tlhintoq.GDI.Graphics.Save(Image ImageObjectToSave, String FilePath)
-
{
-
if (MakeDirForFIle(FilePath)) /* return true if folder exists, making if needed */
-
{
-
string NewFilePath = UniqeFilename(FilePath);//This reserves the name
-
switch (System.IO.Path.GetExtension(FilePath).ToLower().TrimStart('.'))
-
{
-
case "jpg":
-
SaveJPG(ImageObjectToSave, NewFilePath);
-
break;
-
case "bmp"
-
// Call bitmap save function
-
break;
-
case "tga"
-
// Call Targa save function
-
break;
-
}
-
}
-
}
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?
@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 :)
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
@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?
@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...
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |