简体   繁体   English

Server.MapPath实现

[英]Server.MapPath Implementation

I am getting an error on the the file path to create the log file. 我在创建日志文件的文件路径上遇到错误。 I need to use Server.MapPath to implement a correct path but have never used one. 我需要使用Server.MapPath来实现正确的路径,但从未使用过。 Any suggestions? 有什么建议么?

Code: 码:

FileStream fs = new FileStream(
                    Path.Combine(LogExtensionConfigSettings.LogFilePath, 
                                 "VanickWebServiceLogger.txt"), 
                                 FileMode.Append, 
                                 FileAccess.Write);

Server.MapPath maps a virtual file path to a physical one - if LogFilePath is already a physical path them MapPath in unnecessary. Server.MapPath虚拟文件路径映射到物理路径-如果LogFilePath 已经是物理路径,则不需要MapPath

If it's a virtual path then just call Server.MapPath on the virtual path: 如果是虚拟路径,则只需在虚拟路径上调用Server.MapPath

string path = Server.MapPath(Path.Combine(LogExtensionConfigSettings.LogFilePath, 
                                 "VanickWebServiceLogger.txt");

FileStream fs = new FileStream(  path, 
                                 FileMode.Append, 
                                 FileAccess.Write);

Note that you should also enclose the FileStream in a using statement to ensire it gets closed if there is an exception: 请注意,还应将FileStream包含在using语句中,以确保在发生异常时将其关闭:

using(FileStream fs = new FileStream(path, 
                                     FileMode.Append, 
                                     FileAccess.Write)
{
    // do stuff 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM