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
Solved

Error using StreamWriter class

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 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