我总觉的,贴了这段代码,是一件很‘杯具’的事情。
作为一个iphone的码农,竟然就这样,懒得手写添加plist文件,而用代码生成。需求呢。就是把当前文件夹里面所有的某种或者某几种类型的文件,做一个目录。本例中,用的是txt当作示范。最终生成一个标准的,可以在xcode里面使用的一个plist文件。其实后来想想,完全可以使用ObjectiveC 实现,但是对于c#熟悉程度确实要好一点。。。
代码
class
Program
{
static void Main( string [] args)
{
// Create the file and writer.
FileStream fs = new FileStream( " info.plist " , FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
// Start the document.
w.WriteStartDocument();
// Add commets
w.WriteComment( " DOCTYPE plist PUBLIC \ " - // Apple // DTD PLIST 1.0 // EN\" \" http://www.apple.com/DTDs/PropertyList-1.0.dtd ");
// PLIST START
w.WriteStartElement( " plist " );
// PLIST VERSION
w.WriteAttributeString( " version " , " 1.0 " );
w.WriteStartElement( " array " );
string pathName = System.Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(pathName);
FileSystemInfo[] fsi = di.GetFileSystemInfos();
foreach (FileSystemInfo fi in fsi)
{
if (fi.Extension.Contains( " .txt " ))
{
// Write a key string pair
w.WriteStartElement( " dict " );
w.WriteElementString( " key " , fi.Name.ToString());
w.WriteElementString( " string " , fi.Name.ToString());
w.WriteEndElement();
}
}
// End the document.
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
}
}
{
static void Main( string [] args)
{
// Create the file and writer.
FileStream fs = new FileStream( " info.plist " , FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
// Start the document.
w.WriteStartDocument();
// Add commets
w.WriteComment( " DOCTYPE plist PUBLIC \ " - // Apple // DTD PLIST 1.0 // EN\" \" http://www.apple.com/DTDs/PropertyList-1.0.dtd ");
// PLIST START
w.WriteStartElement( " plist " );
// PLIST VERSION
w.WriteAttributeString( " version " , " 1.0 " );
w.WriteStartElement( " array " );
string pathName = System.Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(pathName);
FileSystemInfo[] fsi = di.GetFileSystemInfos();
foreach (FileSystemInfo fi in fsi)
{
if (fi.Extension.Contains( " .txt " ))
{
// Write a key string pair
w.WriteStartElement( " dict " );
w.WriteElementString( " key " , fi.Name.ToString());
w.WriteElementString( " string " , fi.Name.ToString());
w.WriteEndElement();
}
}
// End the document.
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
}
}