OK, here's the code, with a bit of explanation. I appreciate any help!
In the main routine I call a method to perform the transformation,
followed by a method to transfer the generated file via FTP:
sErrorMsg = objRecipient.Tr ansform();
sErrorMsg = objRecipient.Ou tputToFTP();
The Transform() method is as follows:
internal string Transform()
{
if (dsResults.Tabl es.Count 0)
{
// Convert to XML
string xmlResultsFileP ath = String.Empty;
XmlDataDocument xmlResults = new XmlDataDocument (dsResults);
string xmlResultsFilen ame = Path.ChangeExte nsion("Data", ".xml");
string xmlResultsFileP ath = Path.Combine(re pository,
xmlResultsFilen ame);
xmlResults.Save (xmlResultsFile Path);
// Determine the path to the installation
Module mod = Assembly.GetExe cutingAssembly( ).GetModules()[0];
string fullPath = mod.FullyQualif iedName;
string myInstallPath = Path.GetDirecto ryName(fullPath );
// Transform each output object
for (int i = 0; i < outputs.Count; i++)
{
Output o = (outputs[i] as Output);
// Create the XsltSettings object with document() and scripting
enabled
XsltSettings settings = new XsltSettings(tr ue, true);
// Execute the transform
XslCompiledTran sform xslt = new XslCompiledTran sform();
xslt.Load(Path. Combine(myInsta llPath, o.Transformatio n),
settings, new XmlUrlResolver( ));
xslt.Transform( xmlResultsFileP ath, Path.Combine(re pository,
o.OutputFilenam e));
}
}
return String.Empty;
}
The OutputToFTP() method is as follows. It uses an Indy component to do
the FTPing (the code is pre-.NET 2.0):
internal string OutputToFTP()
{
for (int i = 0; i < outputs.Count; i++)
{
Output o = (outputs[i] as Output);
if (o.OutputToFtp)
{
FileInfo fi = new FileInfo(Path.C ombine(reposito ry,
o.OutputFilenam e));
if (fi.Exists)
{
FTP indyFTP = new FTP();
try
{
indyFTP.Host = o.FtpHost;
indyFTP.Usernam e = o.FtpUsername;
indyFTP.Passwor d = o.FtpPassword;
indyFTP.Connect ();
indyFTP.ChangeD ir("in");
indyFTP.Put(Pat h.Combine(repos itory, o.OutputFilenam e),
o.OutputFilenam e, false);
}
catch (Exception e)
{
return String.Format(" Error: {0} " +
"when trying to FTP to: {1}. ",
e.Message, o.FtpHost);
}
finally
{
indyFTP.Disconn ect();
}
}
}
}
return String.Empty;
}
If the filestream is still open this suggests a problem with the
Transform() method (though the code was converted from an example in
the VS Help).
Thanks in advance,
Stuart.
st**********@gm ail.com wrote:
But isn't it odd that the file can be copied locally, or attached to an
e-mail?
I'll post up the code just as soon as I'm back from my holiday!
Thanks
Stuart
Peter wrote:
Sounds to me like you aren't closing the filestream after you finish writing
the file.
If you want, post a short-but-complete code sample and somebody will verify
it for you.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
"st**********@g mail.com" wrote:
Dear all
>
I have a Windows Service that retrieves some data as XML, applies an
XSLT to produce a CSV which is saved to the filesystem as a text file.
This all works fine. Depending upon the configuration, this file is
then
1. copied to another location
2. attached to an e-mail message
3. FTPed to a particular location
>
Problem is that the FTPing is failing with the "being used by another
process" error.
>
I assume that the file has finished being written as steps 1 and 2 are
successful. If I try and delete the file manually through Explorer I'm
unable to do so - until I stop the Windows Service.
>
Why does the Windows Service appear to keep a handle on the created
file?
>
Any help is greatly appreciated.
Regards
Stuart
>
>