【问题标题】:How to get just the name of the file from a full directory filename?如何从完整目录文件名中获取文件名?
【发布时间】:2012-01-26 12:30:28
【问题描述】:

我有一个函数可以将文件名和零件号插入表中。但是插入的文件名包括整个目录,例如c:\:文件夹\上传\xml\file.xml

我只想要file.xml

string source = System.Web.HttpContext.Current.Server.MapPath("/uploads/xml/");
        part retrun = null;
        string[] fileEntries = Directory.GetFiles(source);
        foreach (string fileName in fileEntries)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(file));
            Stream reader = new FileStream(fileName, FileMode.Open);
            file f = (file)serializer.Deserialize(reader);
            string part = "";
            foreach (part p in f.@class.part)
            {
                part = p.tcpn.ToString();
                XMLProductQueries.InsertIntoXMLProducts(context, fileName, part);

            }
            reader.Close();
        } 

帮助?

【问题讨论】:

    标签: c# directory filenames


    【解决方案1】:

    使用Path.GetFileName(fileName) 从路径中提取文件名。

    Path.GetFileName(@"c:\:folder\uploads\xml\file.xml") // => returns file.xml 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多