473,898 Members | 2,178 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 2030
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
5300
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
2287
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
2186
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
1451
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
15001
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
8863
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
3088
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
3319
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
11254
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
10854
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...
0
10480
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9658
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...
1
8034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7187
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();...
1
4701
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
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3303
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.