0

Why using Server.MapPath() causing the following error:

Could not find a part of the path 'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.

Here is my code :

 public ActionResult Index(int? page, string sort, string filter)
 {
     try
     {
         string path1 = Path.Combine(Server.MapPath("~/SomeFolder/myFiles/"));
         if (!System.IO.File.Exists(path1))
         {
             string createText = "Hello and Welcome" + Environment.NewLine;
             System.IO.File.WriteAllText(path1, createText);
         }

     }
     catch (Exception ex)
     {
        throw new Exception("HomeController/save text to  file: " + ex.Message, ex);
     }
 }

And here is stacktrace:

ErrorMessage: HomeController/save text to file: Could not find a part of the path 'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.<br/>
InnerException: Could not find a part of the path 'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.
StackTrace:    at myProject.Controllers.HomeController.Index()
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Date :11/19/2017 9:27:50 AM

----------------------------------

How can I fix it?

user3748973
  • 449
  • 2
  • 8
  • 24
  • Forgive me, but is that a unix path trying to start in the home directory, while running on a windows machine? – theMayer Nov 18 '17 at 21:33
  • Also, I don't think your error is coming from Server.MapPath – theMayer Nov 18 '17 at 21:36
  • 3
    Possible duplicate of ["Could not find a part of the path" error message](https://stackoverflow.com/questions/21796687/could-not-find-a-part-of-the-path-error-message) – theMayer Nov 18 '17 at 21:39
  • @theMayer I update my question, please see it again. – user3748973 Nov 19 '17 at 06:23
  • If you have access to the physical file system, see whether `c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\ ` exists (and if not, where things go wrong). Secondly, check that `Images/timaitUsersImages/` isn't actually just a controller route or such. – StuartLC Nov 19 '17 at 06:28
  • `Images/timaitUsersImages/` actually doesn't exist. it's mt fault. I update my question, please see it again. – user3748973 Nov 19 '17 at 07:13

1 Answers1

2

Your code seems a bit buggy

You've got a Path.Combine In there but only one folder path as an argument- it'll work but combine is for combining path parts. Are you sure there's no path part missing, like more folders, or a file name?

Next, you Server.MapPath to a folder, but check its existence with File.Exists - This won't work, either make it a path to a file, or check it with Directory.Exists

Server.MapPath doesn't care about non existing folders and files. It simply tells you what the absolute path would be for the relative file you pass it. It doesn't check anything about it. If you plan to write a file to this path, you can safely call Directory.CreateDirectory on it first to ensure the directory exists before writing the file to it. If the dir already exists, this call is a non-op. For example:

var fullPath= Server.MapPath("/non/existing/folders/");
Directory.CreateDirectory(fullPath);
fullPath=Path.Combine(fullPath,"newfile.txt");
File.WriteAllText(fullPath, content);
Caius Jard
  • 72,509
  • 5
  • 49
  • 80