译者手记: 因为这两篇都是关于 System.IO.Path 下面的方法, 所以放在一篇方便查阅.

 

 ========================

译自: http://dotnettipoftheday.org/tips/SystemIOPathCombine.aspx

 

让我们来看看下面这段创建一个文件路径的代码:

    public string GetFullPath(string fileName)

    {

        string folder = ConfigurationManager.AppSettings["MyFolder"];

        return folder + fileName;

    }

这段代码很可能导致错误. 比如, 当你设置文件夹配置, 你必须记住确保它以斜线结尾. 为了避免这种问题, 使用 Path.Combine() 方法来确保文件夹(的路径)有结束斜线:

    public string GetFullPath(string filename)

    {

        string folder = ConfigurationManager.AppSettings["MyFolder"];

        return Path.Combine(folder, filename);

    }

 

 ======================================================================

下面这段译自:http://dotnettipoftheday.org/tips/PathGetRandomFileName.aspx

 

 

当处理临时文件的时候,使用 Path.GetRandomFileName() 或者 Path.GetTempFileName()
(Use Path.GetRandomFileName() or Path.GetTempFileName() when working with temp files)

不要重新实现为临时文件产生唯一的名字的函数. 使用下面其中一个已经存在的方法:

9/5/2007 

 

 

相关文章: