Solved

Error using StreamWriter class

Posted on 2015-02-09
2
141 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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