strFilePath = System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)
strFilePath = str + "ApInterface_" + Format(Now.Date, "dd-MMM-yyyy").Replace("-", "") + "_" + Format(Now, "HH:mm:ss").Replace(":", "") + ".dat"
我有上面的代码片段,其中将文件保存为.dat扩展名的指定文件夹。对我来说,问题在于道路。当我指定类似“ D:\ myfolder”之类的路径时,将导出数据并打开文件,但不会保存它。如果我将文件夹指定为“ D:\ myfolder \”,则可以完美保存,为什么我需要"\"
和结尾?
使用Path.Combine而不是字符串连接。它将在需要时添加斜杠。
您的代码应该不是这样的东西:
strFilePath = System.Web.HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath)
strFilePath = strFilePath + "ApInterface_" + Format(Now.Date, "dd-MMM-yyyy").Replace("-", "") + "_" + Format(Now, "HH:mm:ss").Replace(":", "") + ".dat"
就目前而言,没有解释“ str”的含义。
假设以上是正确的,您之所以需要“ \”是因为没有它,就有两个截然不同的路径:
[第一个引用了“ D”驱动器的根目录中的一个名为“ myfolderApInterface_01012001_010101.dat”的文件,而第二个引用了“ D”驱动器的“ myfolder”目录中的一个名为“ ApInterface_01012001_010101.dat”的文件。 >
正如其他人提到的,您可以使用Path.Combine来确定是否已经存在“ \”,并仅在需要时才添加它。
P.S。您可能还需要考虑使用string.Format来建立文件名以提高可读性