簡體   English   中英

c#使用Server.MapPath()找不到路徑的一部分

[英]c# Could not find a part of the path with Server.MapPath()

為什么使用Server.MapPath()導致以下錯誤:

找不到路徑“ c:\\ wwwroot \\ currentuser \\ example.com \\ wwwroot \\ SomeFolder \\ myFiles \\”的一部分。

這是我的代碼:

 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);
     }
 }

這是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

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

我該如何解決?

您的代碼似乎有問題

您在其中有一個Path.Combine ,但是只有一個文件夾路徑作為參數-可以使用,但Path.Combine是用於組合路徑部分的。 您確定沒有路徑部分丟失,例如更多文件夾或文件名嗎?

接下來,將Server.MapPath移至文件夾,但使用File.Exists檢查其是否存在-這將不起作用,將其設置為文件的路徑,或者使用Directory.Exists檢查

Server.MapPath不在乎不存在的文件夾和文件。 它只是告訴您傳遞的相對文件的絕對路徑是什么。 它不會對此進行任何檢查。 如果計划將文件寫入此路徑,則可以先安全地在其上調用Directory.CreateDirectory ,以確保目錄存在,然后再將文件寫入其中。 如果該目錄已經存在,則此調用為非操作。 例如:

var fullPath= Server.MapPath("/non/existing/folders/");
Directory.CreateDirectory(fullPath);
fullPath=Path.Combine(fullPath,"newfile.txt");
File.WriteAllText(fullPath, content);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM