-1

In an application, the user may open saved files. My goal is to check wether a file is already open, and offer the user the possibility to open it again only if a local copy is created, so that the same file cannot be modified at the same time.

The workflow would be as follows:

if (File.Exists(strFileName))
   bCreateCopy = AlertBox.Display("File already open. Work on a copy?", true/false);

if (bCreateCopy == true)
{
    strNewAutomaticFileName = createAutomaticFileName (sourceFile)
    File.Copy(sourceFile, strNewAutomaticFileName );
}

Is there a method that does what I need in 'createAutomaticFileName()' ?

I was thinking of creating the typical cannonical names:

 sourceFile - copy
 sourceFile - copy (1)
 sourceFile - copy (2)

Is there a better workaround to accomplish this purpose?

Slash
  • 285
  • 2
  • 13
  • 1
    How does `File.Exists()` indicate that the file is open? – itsme86 Jun 15 '16 at 15:12
  • Do some research dude. There are plenty of answers if you just take 2 minutes to look. http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use AND http://stackoverflow.com/questions/13049732/automatically-rename-a-file-if-it-already-exists-in-windows-way – Rick S Jun 15 '16 at 15:15
  • what language this code was actually writen? – Anton Semenov Jun 15 '16 at 15:15
  • Your probably want the extension after the copy (1) right? – omerts Jun 15 '16 at 15:20
  • You are absolutely right @RickS, I was in a hurry and did not find that reference at all. [13049732](http://stackoverflow.com/questions/13049732/automatically-rename-a-file-if-it-already-exists-in-windows-way) has the answer. – Slash Jun 16 '16 at 11:02

2 Answers2

1

If I proper understood you then try something like:

public string createAutomaticFileName(string sourceFile)
{
   var done = true;
   int i = 0;       
   while (done)
   {
        var newFileName = (i == 0) ? string.Format("{0} - copy ", sourceFile) : string.Format("{0} - copy ({1})", sourceFile, i.ToString());
        if (!File.Exists(newFileName)) 
        {
            return newFileName;
        }
        i++;
    }
}
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

This will also suffix the extension:

if (File.Exists(strFileName))
{
   bCreateCopy = AlertBox.Display("File already open. Work on a copy?", true/false);
}

if (bCreateCopy == true)
{
    strNewAutomaticFileName = createAutomaticFileName(sourceFile)
    File.Copy(sourceFile, strNewAutomaticFileName );
}

...

private string createAutomaticFileName(string filePath, int count = 1)
{
    var pathWithoutExtension = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
    var newFilePath = String.Format("{0} - copy ({1}){2}", pathWithoutExtension, count, Path.GetExtension(filePath));

    if (File.Exists(newFilePath))
    {
        return createAutomaticFileName(filePath, ++count);
    }

    return newFilePath;
}

Of course you need to use it after you already know the initial file exists, like in your example.

omerts
  • 8,485
  • 2
  • 32
  • 39