Solved

Error using StreamWriter class

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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