Search code examples
c#svnsharpsvn

The file being use by another process when i add it programatically by SharpSvn


We are using SharpSvn to add SolidWorks files programatically to SVN tortoise. When file is open in SolidWorks, i want to add it to SVN by code without closing file. I used code below

        var SvnResult = new SvnResult();
        var FullPath = SvnHelper.FileCombine(FileName);

        try
        {

            var SvnArg = new SvnAddArgs();
            SvnArg.Force = true;
            SvnArg.Depth = SvnDepth.Infinity;
            //
            SvnClient.Add(FullPath, SvnArg);

            SvnResult.Message = "Success.";
            SvnResult.Status = true;
            //
            return SvnResult;
        }
        catch (SvnException exc)
        {
            SvnResult.Message = exc.Message;
            SvnResult.Status = false;
            return SvnResult;
        }

and i get error like this : The process cannot access the file because it is being used by another process.

How can i add it to SVN without closing file? Regards,


Solution

  • We solved the problem. At first we used TortoiseSvn.exe command lines to add and commit the file but when we used to send commit command, svn Dialog form was raised. For solving this problem I install “Command Line Client Tools” from the svn setup. By installing this option you can find svn.exe under svn path “C:\Program Files\TortoiseSVN\bin”. I add this path to Environment Variables and then use svn command lines to add and commit while file is open.

    public SvnResult CommitFiles_BySVNCmd(string filePath)
        {
            var fullPath = SvnHelper.FileCombine(filePath);
            var svnResult = new SvnResult();
    
            try
            {
                // svn add command
                var status = GetStatus(fullPath);
                //
                if (status.LocalContentStatus == SvnStatus.NotVersioned)
                {
    
                    var argumentsAdd = $@"add {fullPath}";
                    ProcessStart(argumentsAdd);
                }
    
    
                // svn commit command
                var argumentsCommit = $@"commit -m Commited_Automatically {fullPath}";
                ProcessStart(argumentsCommit);
    
    
                svnResult.Message = "Success
                svnResult.Status = true;
    
                return svnResult;
    
            }
            catch (SvnException se)
            {
    
                svnResult.Message = se.Message;
                svnResult.Status = false;
    
                return svnResult;
            }
        }
    
    private void ProcessStart(string arguments)
    {
         var processInfo = new ProcessStartInfo("svn", arguments);
         processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
         Process.Start(processInfo);
    }
    

    Best Regards,