473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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(strLogFile, FileMode.Append,
FileAccess.Write, FileShare.None)
If strFileName.CanWrite Then
Dim w As StreamWriter = New StreamWriter(strFileName)
w.WriteLine(DateTime.Now.ToShortDateString & vbTab &
DateTime.Now.ToLongTimeString & vbTab & logMessage)
w.Flush()
w.Close()
End If
End Sub

Apr 4 '07 #1
1 4253
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=1970833

WBR, Alex Meleta

-----Original Message-----
From: jtertin [mailto:jt*****@gmail.com]
Posted At: Thursday, April 05, 2007 2:54 AM
Posted To: microsoft.public.dotnet.general
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(strLogFile, FileMode.Append,
FileAccess.Write, FileShare.None)
If strFileName.CanWrite Then
Dim w As StreamWriter = New StreamWriter(strFileName)
w.WriteLine(DateTime.Now.ToShortDateString & vbTab &
DateTime.Now.ToLongTimeString & 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
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...
3
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...
1
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
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...
0
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...
8
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" ...
7
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...
1
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...

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.