Saving to a Unique Filename

G

Guest

I'm working in Visual Studio 2005 and C#.

I'm working in an application that automatically saves a file to a specific
directory.

I've seen other applications that can detect if a file of the same name
already exists, and if so, adds a number to the filename. For example, if
foo.doc already exists, the application is smart enough to save the new file
as foo[1].doc.

Is there some bit of magic in FileWriter to ensure saving to a unique name,
or do I need to use something like FileWatcher and add my own version number
to ensure a unique filename?

Thanks,
Randy
 
N

Nicholas Paldino [.NET/C# MVP]

Randy,

No, all you need to do is check for the existence of the file before you
save through a call to the static Exists method on the File class. If it
exists, modify the file name, and check again, if that exists, modify again,
and so on, and so on, until you have a unique file name.
 
P

Peter Duniho

randy1200 said:
[...]
I've seen other applications that can detect if a file of the same name
already exists, and if so, adds a number to the filename. For example, if
foo.doc already exists, the application is smart enough to save the new file
as foo[1].doc.

Is there some bit of magic in FileWriter to ensure saving to a unique name,
or do I need to use something like FileWatcher and add my own version number
to ensure a unique filename?

There's no magic. But you don't need to use FileWatcher (in fact, I
don't see how FileWatcher will help).

Basically, you just need to attempt to create the file. If that fails
and it's because the file already exists, try the next filename in the
sequence. For example:

FileStream FsCreateNewNumbered(string strFilename)
{
int ifile = 0;
FileStream fsRet = null;
string strFileTemplate =
Path.GetFileNameWithoutExtension(strFilename) +
"[{0}]" + Path.GetExtension(strFilename);

while (fsRet != null)
{
string strFileCur;

if (ifile > 0)
{
strFileCur = string.Format(strFileTemplate, ifile);
}
else
{
strFileCur = strFilename;
}

try
{
fsRet = new FileStream(strFileCur, FileMode.CreateNew);
}
catch (IOException)
{
if (!File.Exists(strFileCur))
{
throw;
}
}

ifile++;
}

return fsRet;
}

Notes:
1) I didn't compile the above; minor fixes might be required. :)
2) You'll note that the code doesn't bother to check if the file
exists before trying to create it. There's no point in doing so, since
the file could be created by some other process between the time you
check and the time you try to create it, so you still need the check for
existence in the exception handler. So checking for existence before
creating it doesn't help.

Hope that helps.

Pete
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

randy1200 said:
I'm working in Visual Studio 2005 and C#.

I'm working in an application that automatically saves a file to a
specific
directory.

I've seen other applications that can detect if a file of the same name
already exists, and if so, adds a number to the filename. For example, if
foo.doc already exists, the application is smart enough to save the new
file
as foo[1].doc.

Is there some bit of magic in FileWriter to ensure saving to a unique
name,
or do I need to use something like FileWatcher and add my own version
number
to ensure a unique filename?

The magic is check if a file with the given template exist, if so start
creating the next file following your schema until the file is not found.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top