首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#无法使用Server.MapPath()找到路径的一部分

c#无法使用Server.MapPath()找到路径的一部分
EN

Stack Overflow用户
提问于 2017-11-18 21:05:26
回答 1查看 821关注 0票数 0

为什么使用Server.MapPath()会导致以下错误:

找不到路径'c:\wwwroot\currentuser\example.com\wwwroot\SomeFolder\myFiles\'.的一部分

这是我的代码:

代码语言:javascript
运行
复制
 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);
     }
 }

下面是堆栈跟踪:

代码语言:javascript
运行
复制
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

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

我怎么才能修好它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-19 07:34:57

你的代码似乎有点错误

这里有一个Path.Combine,但是只有一个文件夹路径作为参数-它可以工作,但是组合是用于组合路径部分。您确定没有缺少路径部分,比如更多的文件夹或文件名吗?

接下来,您可以Server.MapPath到一个文件夹,但是用File.Exists检查它的存在--这是行不通的,要么让它成为文件的路径,要么用Directory.Exists检查它。

Server.MapPath不关心不存在的文件夹和文件。它只是告诉您传递的相对文件的绝对路径是什么。它没有检查任何关于它的东西。如果计划将文件写入此路径,则可以首先安全地调用该路径上的Directory.CreateDirectory,以确保该目录在写入该文件之前存在。如果dir已经存在,则此调用是非操作的。例如:

代码语言:javascript
运行
复制
var fullPath= Server.MapPath("/non/existing/folders/");
Directory.CreateDirectory(fullPath);
fullPath=Path.Combine(fullPath,"newfile.txt");
File.WriteAllText(fullPath, content);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47371036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档