Solved

Error using StreamWriter class

Posted on 2015-02-09
2
137 Views
Last Modified: 2015-02-09
Hi, I'm using VS2012
In my asp.net project I included a class library that has a class with a mehtod called WriteError.  I'm using this to write error log to the C drive at my local pc and later on the web server when it's deployed.

I'm getting an error when executing, "StreamWriter logFile = new StreamWriter(fullFileName, true);"
{"Could not find a part of the path 'c:\\xxxx\\xxxx-Log.txt'."}

I thought the StreamWriter will create a folder and the file if it's not found?  

Thank you.
0
Comment
Question by:lapucca
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 40599151
I believe the reason is pointed out in the error message: "Could not find a part of the path" As far as I remember a stream writer will not create folders, only files.
So you need to check if the folder exists and may be create it manually.

Yes, I just tested  - see the code. I have "AAA" folder but haven't "BBB" folder. In the first case - no problems, in the second - the error you have shown:

        private void button1_Click(object sender, EventArgs e)
        {
            using(StreamWriter sw = new StreamWriter(@"C:\work\AAA\mylog1.txt"))
            {
                sw.WriteLine("Line One ");
                sw.WriteLine("Line Two ");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(@"C:\work\BBB\mylog1.txt"))
            {
                sw.WriteLine("Line Three ");
                sw.WriteLine("Line Four ");
            }

        }

Open in new window


Error:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

Additional information: Could not find a part of the path 'C:\work\BBB\mylog1.txt'.
0
 

Author Closing Comment

by:lapucca
ID: 40599199
I thought it would also create folder.  Okay, that make sense.  Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now