如何解决这个错误“找不到路径的一部分'C:\inetpub \wwwroot \ app_data \client_secret.json'" [英] How to solve this error "could not find a part of the path 'C:\inetpub\wwwroot\app_data\client_secret.json'"
本文介绍了如何解决这个错误“找不到路径的一部分'C:\inetpub \wwwroot \ app_data \client_secret.json'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有使用谷歌日历的问题。
当我调用日历页面时,系统抛出错误
Hi
I have an issue where using the google calendar.
When I call the calendar Page, system throw the error "
"
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\App_Data\client_secret.json'.
请指教我
提前谢谢
maideen
我尝试过:
Please advice me
Thank you in advance
maideen
What I have tried:
<pre>Private Shared gFolder As String = System.Web.HttpContext.Current.Server.MapPath("/App_Data")
Public Shared Function GetClientConfiguration() As GoogleClientSecrets
Using stream = New FileStream(gFolder & "\client_secret.json", FileMode.Open, FileAccess.Read)
Return GoogleClientSecrets.Load(stream)
End Using
End Function
推荐答案
尝试:
Try:
Private Shared gFolder As String = System.Web.HttpContext.Current.Server.MapPath("~/App_Data")
由于无法准确控制共享
字段初始化程序运行,我倾向于避免使用HttpContext.Current
。如果在请求上下文之外初始化类,则最终可能会出现NullReferenceException
,这会使整个类在应用程序的生命周期内无法使用。
相反,映射方法内部的路径,并使用应用程序相对路径作为解决方案1建议:
Since you can't control precisely when aShared
field initializer runs, I'd be inclined to avoid usingHttpContext.Current
from one. If the class is initialized outside of a request context, you could end up with aNullReferenceException
, which would then render your entire class unusable for the lifetime of your application.
Instead, map the path inside the method, and use an app-relative path as Solution 1 suggested:
Public Shared Function GetClientConfiguration() As GoogleClientSecrets
Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
If context Is Nothing Then Throw New InvalidOperationException("Cannot call this method outside of a request.")
Dim filePath As String = context.Server.MapPath("~/App_Data/client_secret.json")
Using stream = File.OpenRead(filePath)
Return GoogleClientSecrets.Load(stream)
End Using
End Function
这篇关于如何解决这个错误“找不到路径的一部分'C:\inetpub \wwwroot \ app_data \client_secret.json'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文