Hi guys,
in a windows environment and in several applications, when we create a new file with the same name of an existing file, the user is not prompted with a message that the output file already exists, but it creates a new file with (1) at the end of the filename...
name.doc
will be
name(1).doc
I'm trying to create the same behavior in C# and my question is... can I reduce all this code?
FileInfo finfo = new FileInfo(fullOutputPath);
if (finfo.Exists)
{
int iFile = 0;
bool exitCreatingFile = false;
while (!exitCreatingFile)
{
iFile++;
if (fullOutputPath.Contains("(" + (iFile - 1) + ")."))
fullOutputPath = fullOutputPath.Replace(
"(" + (iFile - 1) + ").",
"(" + iFile + ")."); // (1) --> (2)
else
fullOutputPath = fullOutputPath.Replace(
Path.GetFileNameWithoutExtension(finfo.Name),
Path.GetFileNameWithoutExtension(finfo.Name) + "(" + iFile + ")"); // name.doc --> name(1).doc
finfo = new FileInfo(fullOutputPath);
if (!finfo.Exists)
exitCreatingFile = true;
}
}