473,327 Members | 1,920 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,327 software developers and data experts.

Write to File after creating it ??? Known issue or my code?

Tat
Hello,
I have an error handling routine in Win app. If there is an error, the app
throws an exception and writes it to the text file all info about the error
so the app can send it to tech. If file doesn't exist to begin with, it
creates it. What happens is that after it creates file, it can't write to it
and blows up with the following: "The process cannot access the file "..."
because it is being used by another process"

Here is the outline of what I do:
FileInfo objFile = new FileInfo(appPath + @"\errors\errorlog.txt");
if(!objFile.Exists)
{
objFile.Create();
}
objFile = null;

objFile = new FileInfo(appPath + @"\errors\errorlog.txt");
StreamWriter sw = objFile.AppendText();
sw.WriteLine("error");
sw.Close();
sw = null;
objFile = null;

I used TextWriter, I didn't use FileInfo object for writing, I tried this
and that.
Please let me know what goes wrong and why app still keeps a hold of a file
that was created and how to work around it if it's a known issue. Needless
to say, if file is already created, it writes ok.
Thanks a bunch,
Tat
Jul 21 '05 #1
1 1402
On Wed, 29 Sep 2004 09:24:36 -0400, Tat wrote:
Please let me know what goes wrong and why app still keeps a hold of a file


The create method, opens a stream for you. Just use code similar to this:

StreamWriter sw;
FileInfo objFile = new FileInfo(appPath + @"\errors\errorlog.txt");
if(!objFile.Exists)
{
sw = objFile.Create();
}
else
{
sw = objFile.AppendText();
}

sw.WriteLine("error");
sw.Close();
sw = null;
objFile = null;

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #2

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

Similar topics

23
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: Paul | last post by:
Hi there, When adding a "File Field" HTML control to an aspx page to facilitate file uploading, the following occurs: 1. You select a file that is larger than the allowed size limit. 2. Once...
1
by: Tat | last post by:
Hello, I have an error handling routine in Win app. If there is an error, the app throws an exception and writes it to the text file all info about the error so the app can send it to tech. If...
11
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
27
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
3
by: =?Utf-8?B?RGFuYQ==?= | last post by:
I am re-posting this message after registering my posting alias. When I specify an end tag for the clear element of namespaces in my web.config file, the parser error "Unrecognized element 'add'"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.