Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to write a exception in text file it takes virtual path its write the file only in virtual path.but i want to write that file in physicl path

What I have tried:

string dir = "D:\\Oracle Logs\\Error.txt";  // folder location
               if (!Directory.Exists(dir))
               {
                   Directory.CreateDirectory(dir);
                   File.AppendAllText(Server.MapPath("D:\\Oracle Logs\\Error.txt"), "Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
              "" + Environment.NewLine + "Date :" + DateTime.Now.ToString() + "WarningException:" + ex.InnerException);
                   string New = Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine;
                   File.AppendAllText(Server.MapPath("D:\\Oracle Logs\\Error.txt"), New);
                   // ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert(objex.Message);", true);

               }

               string filePath = Server.MapPath("D:\\Oracle Logs\\Error.txt");

               using (StreamWriter writer = new StreamWriter(filePath, true))
               {
                   writer.WriteLine("-----------------------------------------------------------------------------");
                   writer.WriteLine("Date : " + DateTime.Now.ToString());
                   writer.WriteLine();

                   while (ex != null)
                   {
                       writer.WriteLine(ex.GetType().FullName);
                       writer.WriteLine("Message : " + ex.Message);
                       writer.WriteLine("StackTrace : " + ex.StackTrace);

                       ex = ex.InnerException;
                   }
               }
Posted
Updated 1-Jul-19 21:27pm
Comments
Richard MacCutchan 2-Jul-19 3:09am    
You cannot access physical paths in a web server as far as I am aware.

You have the path hardcoded multiple times, even though you have it in a variable called "dir" in the first line. The variable name and the comment specify it is a directory but it includes the file name. You then ask for a directory with the filename to be created.

Clean it up, then run it through the debugger to see the path used is as expected, and more importantly which line you end up with the wrong value. And read the documentation: HttpServerUtility.MapPath(String) Method (System.Web) | Microsoft Docs[^] - Why do you call MapPath when you already have the full file system path hard coded?

In case you consider using a debugger to be an advanced topic you will learn later, then change your priorities at once. Learning the basics of a debugger takes 10 minutes and that time is paid back within a day.
 
Share this answer
 
You can't, except by using Server.MapPath as you have. As a website, you are normally not given any access to the "rest of the machine" to prevent you accidentally messing up other sites.
Server.MapPath converts a site-centric path like "~/documents/myDoc.docx" to a physical path on the server: "C:\sites\iis\Yourdomain\main\documents\myDoc.Docx", it won't "translate" a physical address line "D:\Oracle Logs\Error.txt" and give you access to the file.

In addition, you need to be aware that the database server may not even by on the same machine - it rarely is in production - so the folder you want is likely to be elsewhere on the network and probably isn't shared.

And of course, you can't access the client computer file system in any way, shape or form for security reasons.

And do yourself a favour; don't hardcode paths: always store them in config files so the code doesn't need to be changed when they do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900