473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check file for write access before opening with a TextWriter

I am currently using the following code to make sure a file is
writable (i.e. is not in use) by using the CanWrite method of the
FileStream object. Using the code below, the TextWriter is still
trying to write to the file and is throwing an exception (File... in
use by another process) whenever the output file (strFileName) is
open. I am trying to prevent it from writing to it at all if this is
the case... Any insight?
Public Shared Sub Log(ByVal logMessage As String)
Dim strFileName As New FileStream(strL ogFile, FileMode.Append ,
FileAccess.Writ e, FileShare.None)
If strFileName.Can Write Then
Dim w As StreamWriter = New StreamWriter(st rFileName)
w.WriteLine(Dat eTime.Now.ToSho rtDateString & vbTab &
DateTime.Now.To LongTimeString & vbTab & logMessage)
w.Flush()
w.Close()
End If
End Sub

Apr 4 '07 #1
1 4275
The CanWrite checks a possibility to write into StreamWriter.
There are couple of threads regarding files accessibility can be
helpful:

http://www.thescripts.com/forum/thread507071.html
http://www.codeproject.com/useritems...asp?df=100&for
umid=315432&exp =0&select=19708 33

WBR, Alex Meleta

-----Original Message-----
From: jtertin [mailto:jt*****@ gmail.com]
Posted At: Thursday, April 05, 2007 2:54 AM
Posted To: microsoft.publi c.dotnet.genera l
Conversation: Check file for write access before opening with a
TextWriter
Subject: Check file for write access before opening with a TextWriter

I am currently using the following code to make sure a file is
writable (i.e. is not in use) by using the CanWrite method of the
FileStream object. Using the code below, the TextWriter is still
trying to write to the file and is throwing an exception (File... in
use by another process) whenever the output file (strFileName) is
open. I am trying to prevent it from writing to it at all if this is
the case... Any insight?
Public Shared Sub Log(ByVal logMessage As String)
Dim strFileName As New FileStream(strL ogFile, FileMode.Append ,
FileAccess.Writ e, FileShare.None)
If strFileName.Can Write Then
Dim w As StreamWriter = New StreamWriter(st rFileName)
w.WriteLine(Dat eTime.Now.ToSho rtDateString & vbTab &
DateTime.Now.To LongTimeString & vbTab & logMessage)
w.Flush()
w.Close()
End If
End Sub

Apr 6 '07 #2

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

Similar topics

6
3429
by: Fabian Härle | last post by:
Hi, if I write to a tet file via a System.IO.TextWriter the changes to the file are not done before I close the file. Is there a possibility to always save the file with every change? The point is that I want to write to my log file even if my app crashes. As the log file is not closed clearly in such a case, all my information is gone. Regards + thanx very much in advance
3
6358
by: Abhishek | last post by:
Hi! Everyone I have a small issue, if anyone could help me out I need to upload user files to my website thru my web application. something similar to attaching a file to the mail. I know one way of doing it is thru HTTP but that gives a limitation of 4mb I was doing it thru FTP and it worked fine during preliminary testing on the localhost. but when i tried it doing thru intranet it gave me a problem,
1
1331
by: Bishoy George | last post by:
I want a link to a nice documentation about writing to a server file by C# using System.IO; .
4
2211
by: Greg | last post by:
Using the HttpWebRequest I'm downloading a PDF file and am trying to save it to the hard drive, but it keeps ending up corrupt (according to adobe acrobat). I know the PDF file isn't corrupt because I can view it if I save it from IE. I've tried saving it using the BinaryWriter, TextWriter, and StreamWriter. All have produced the corrupt file. Anyone know what the problem is?
0
1665
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same question - but noone seems to answer. Do i need to do anything specific to write to a text file from a windows service, much appreciated.
8
2590
by: Sohan Kamat | last post by:
Hi, I have a webform and I want to create a hyperlink to a text file, so that it opens on internet explorer in a new window. I have added the following <a href="file:///D:/Test/Test.txt" id="hplTest" runat="server" >testhyperlink</a> Now when I click on the hyperlink, nothing is happening. No error. when I open a browser and type file:///D:/Test/Test.txt, the file is opening.
7
1386
by: Hugh Janus | last post by:
Hi all. Is it possible to take a binary file and out put it as text so that I can store it in a text field of a database? And then later, take the text and 're-stream' it as the original binary file. If so, any ideas on where I could start? I cannot store the file in any other format other than text so I can only do a converstion to text and then back from it.
1
2418
by: BiT | last post by:
Hello I Know how to create and add elements to xml file with XmlTextWriter, here is code for example: Dim textWriter As XmlTextWriter = New XmlTextWriter("table.xml", Nothing) ' Opens the document textWriter.WriteStartDocument()
6
248986
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using System; using System.IO; using System.Diagnostics;
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9142
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
8144
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
6722
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
6016
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
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.