473,804 Members | 3,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Service creates a text file but won't let go

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

Dec 23 '06 #1
4 2023
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

Dec 23 '06 #2
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
Dec 26 '06 #3
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
>
>
Jan 2 '07 #4
I solved this by converting the file to a FileStream object, and using
this in the overloaded Put() method (thanks go to the newsgroup
atozedsoftware. indy.protocol.f tp).

I'd still like to know why the Service keeps a hold of the file but I
guess this question is destined to be left hanging...
gingerbbm wrote:
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
Jan 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5294
by: 0to60 | last post by:
I have a windows service that when started creates two threads: one thread that runs a TcpListener waiting for TcpClients to connect, and another thread that reads from the resulting sockets. My stop code .Abort()s the threads and .Stop()s the listener. One one of my computers, the service runs like, well...a service. It starts and stops nicely. On a different computer (eventually this service will be running on 50+ machines) the...
2
2282
by: Bonnett | last post by:
I have made a simple console application that creates a webpage from my playlist, then uploads it to my webhost. I decided to convert it to a windows service and use the FileSystemWatcher to monitor the playlist file, this is where i became stuck. I know the service detects changes to the playlist as when running the Change event of FileSystemWatcher, I add an item to the event log. I've tried making the service without including the...
6
2182
by: Nathan Kovac | last post by:
Yesterday afternoon I was getting the following errors in a windows service: 'DatabaseManager.DataComponent', 'Error', '3 Errors: Line: 0 - Metadata file 'ScriptingMethods.dll' could not be found Line: 0 - Metadata file 'RemoteLoader.dll' could not be found Line: 0 - Metadata file 'wwScripting.dll' could not be found' Service ran all night fine. This morning I reconnected to the process and the errors are no longer there. Only thing...
1
1448
by: gb | last post by:
I've written a Windows Service program that monitors a directory using the FileSystemWatcher class. The monitoring is done on it's own thread. Once a particular files gets added(created) to the directory, the program calls a routine using ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf module.sub)). That routine creates an object of another class. The problem is that classes' Finalize method is not called until the Windows Service is...
3
14966
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim swLog As StreamWriter = File.CreateText("C:\myLog.txt") swLog.WriteLine("My Windows Service has just started.") swLog.Close() : swLog.Flush() End Sub
4
8849
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a windows service that converts MS Word Files into .PDF files and after that we want to zip the .PDF files. Our code: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set...
2
3086
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in this instance BLAH.log. The code is executed within a timer which runs at 30 second intervals and polls folders. Each part of the process will log to the log file. The service will throw an IOException exception. See below for this and my...
5
3312
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.