Solved

Error using StreamWriter class

Posted on 2015-02-09
2
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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