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

Home Posts Topics Members FAQ

Creating a File using StreamWriter throw exception ...

Hi,

My very simple application VB.NET Visual Studio 2003 console application
uses a log file to write some text to it. I am using StreamWriter object to
create and open and then append text to the log file. Now what happens is
that if i have the folder in which the stream object creates the log file
open in windows explorer, i can see that it creates the log file but then it
crashes, strnage , can somebody tell me how i can stop this from happening.
Below is the function that does it. Seems it crashes at the line where i have
********. Also this error occurs only when the log file does not exit. Once
it has created the log file and the application is restarted, then there is
no problem even if the folder is open in windows explore.

thanks,

The error message is :

"System.IO.IOEx ception: The process cannot access the file
"C:\projects\vb .net\saga\test\ sagalog.txt" because it is being used by
another process."

The function code is :

Public Function WriteToLogfile( ByVal Text As String) As String

Dim logfile As StreamWriter
Dim Logfilepath As String =
ConfigurationSe ttings.AppSetti ngs("LogFilePat handName")
Dim File As File

Try

If Not File.Exists(Log filepath) Then File.Create(Log filepath)
*************** logfile = New StreamWriter(Lo gfilepath, True)
'"SAGALogfile.t xt")
logfile.WriteLi ne(Text)
logfile.Close()

Catch ex As Exception

Finally

'logfile.Close( )

End Try

End Function
Dec 15 '05 #1
2 3371
I can only guess that because File.Create() returns a FileStream, when you
go to write to the file, you get the IOException.

When you create a new instance of the StreamWriter class, if the specified
file name doesn't exist, it will be created. And, of course you are flagging
to append to the file if it exists (hence why it works the second time you
run it).

Hope this helps.
Cheers,
Brendan.

"sameer" <sa****@discuss ions.microsoft. com> wrote in message
news:29******** *************** ***********@mic rosoft.com...
Hi,

My very simple application VB.NET Visual Studio 2003 console application
uses a log file to write some text to it. I am using StreamWriter object
to
create and open and then append text to the log file. Now what happens is
that if i have the folder in which the stream object creates the log file
open in windows explorer, i can see that it creates the log file but then
it
crashes, strnage , can somebody tell me how i can stop this from
happening.
Below is the function that does it. Seems it crashes at the line where i
have
********. Also this error occurs only when the log file does not exit.
Once
it has created the log file and the application is restarted, then there
is
no problem even if the folder is open in windows explore.

thanks,

The error message is :

"System.IO.IOEx ception: The process cannot access the file
"C:\projects\vb .net\saga\test\ sagalog.txt" because it is being used by
another process."

The function code is :

Public Function WriteToLogfile( ByVal Text As String) As String

Dim logfile As StreamWriter
Dim Logfilepath As String =
ConfigurationSe ttings.AppSetti ngs("LogFilePat handName")
Dim File As File

Try

If Not File.Exists(Log filepath) Then File.Create(Log filepath)
*************** logfile = New StreamWriter(Lo gfilepath, True)
'"SAGALogfile.t xt")
logfile.WriteLi ne(Text)
logfile.Close()

Catch ex As Exception

Finally

'logfile.Close( )

End Try

End Function

Dec 15 '05 #2
File.Create returns a FileStream. You have not yet closed it.
In fact, you can remove the whole line:
If Not File.Exists(Log filepath) Then File.Create(Log filepath).
The StreamWriter will create the file for you it it is not there.

Hope this helps,
Thi

Dec 15 '05 #3

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

Similar topics

2
20730
by: Marge Inoferror | last post by:
Visual C#... I'd like to save some settings that are in integers and then save some lists of file names. I'm just starting off (as you can tell - no doubt.). I'm starting by trying to save two integers: numRows and numCols. I put a saveFileDialog in my design view. When it's clicked I do this: private void clickSaveFile(object sender, System.EventArgs e)
9
8281
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
3
2354
by: W Akthar | last post by:
Hi I am trying to create a windows service which queries SQL Server on timed intervals and depending on the results send appointments to Outlook. The problem lies when I try to create an outlook application object.
1
19970
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. Thanks in advance
1
24312
by: steve | last post by:
Hi all, Here's some work in progress that should allow you to run a batch file as a custom action in a VS deployment project. Yup I know you can use js or wsh, but the target may not have either.. Essentially it's just a wrapper for the Process class and a command interpreter. Warning, it only partly works. I had wanted to pass in (a) The name of the batch file (through "BatchFileName"), and
1
3730
by: lactaseman | last post by:
While I know this is not the correct venue... I realize this is of little to no importance to most out there... however, if I had found this in my initial searches, I would have used this. So, as an alternative to the mentalis.org's IniReader, I submit the following files to the web news group DBs...: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6
1782
by: Charlie Bear | last post by:
i'm really stuck with this one can anyone help! i have a website that uses c#. it creates a series of directories and files from an xml source. when the xml changes, the directory that the change applies to is deleted and recreated. All the children for that directory are also re created (as the parent directory then has a different name). the problem i have is that *sometimes* the files or directories don't get created. It either...
4
8930
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
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...
0
10571
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
10326
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
10317
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
10075
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
9143
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
7615
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.