SaveAs Method - Overwriting existing file

E

Erl Egestad

If I invoke the SaveAs method on a fully trusted form and via script save the
with a name of an existing InfoPath XML file (in essence I want to overwrite
an "old" version of the file's XML data), will InfoPath be prompted with the
standard Office - "Are you sure want to overwrite an existing file" message
box?

I want to prevent the user from having ANY control over the Save process
entirely?

Thanks!
 
S

Steve van Dongen [MSFT]

If I invoke the SaveAs method on a fully trusted form and via script save the
with a name of an existing InfoPath XML file (in essence I want to overwrite
an "old" version of the file's XML data), will InfoPath be prompted with the
standard Office - "Are you sure want to overwrite an existing file" message
box?

I want to prevent the user from having ANY control over the Save process
entirely?

<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsmthfileexists.asp
/>

You can use the FileSystemObject to check whether the file exists and
delete it before saving:
if (fso.FileExists(filespec))
fso.DelteFile(filespec);

Or if you want to cover all the bases and do it right, it might look
something like this

var f;
var attr = 0;

try
{
if (fso.FileExists(filespec))
{
f = fso.GetFile(filespec);
attr = f.attributes;
f.Move( <some temp backup file> );
}
XDocument.SaveAs(filespec);
fso.DeleteFile( <temp backup file> );
}
catch (ex)
{
if (fso.FileExists( <backup filename> )
{
fso.MoveFile( <backup filename>, filespec);
f.GetFile(filespec);
f.attributes = attr;
}
}

Regards,
Steve
 

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