解析文本文件并仅保存歌曲名称和文件路径。 [英] Parse a text file and save song names and file paths only.

查看:117
本文介绍了解析文本文件并仅保存歌曲名称和文件路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个我正在制作的程序,它看起来虽然这里的文件是文件的例子

Hello i have a program i am making and it looks though a file here is a exmaple of the file

  %Karaoke Bad Manners- My girl lollopopNo DatacC:\Users\ELFENLIEDTOPFAN5\Desktop\KARAOKE\KARAOKE\SCR JIM\Karaoke Bad Manners- My girl lollopop.avi
8:58:32 PMV   monkey man karaokeNo DataPC:\Users\ELFENLIEDTOPFAN5\Desktop\KARAOKE\KARAOKE\SCR JIM\monkey man karaoke.avi V   #Young Turks   Stewart Rod [Karaoke]No DataaC:\Users\ELFENLIEDTOPFAN5\Desktop\KARAOKE\KARAOKE\SCR JIM\Young Turks   Stewart Rod [Karaoke].avi V 




正如你所看到的那样,它有一个歌曲名称,旁边是歌曲的路径 

as you can see form the example it has the song name and next to it the path of the song 

是否有办法搜索整个文件获取歌曲名称。 。并改变每首歌曲路径的路径,以便更好地理解 

is there a way to search this whole file get the song name.. and change the path of each song path so for better understanding 

Karaoke Bad Manners- My girl lollopopNo DatacC:\Users\ELFENLIEDTOPFAN5\Desktop\KARAOKE\KARAOKE\SCR JIM\Karaoke Bad Manners- My girl lollopop.avi

所以我希望我的程序能够获得列出的每首歌曲的路径,然后保存路径,然后再用新路径重新编写文件,因为该程序存储了用户名elfenliedtopfan5。这里的女巫不会在其他电脑上相同所以有没有办法解析
虽然每个歌曲的名称和路径,并将路径存储在文本文件或类似的东西,然后当你加载程序时它会保持当您在taget pc上加载它时,它将保存路径以更新路径

so i want my program to get the path to each song listed and just save the path then later on re wright the file with a new path because this program stores the username elfenliedtopfan5. here witch wont be the same on others pcs so is there a way to parse though each song name and path to it and store the path on text document or something like that and then when you load up the program it will keep that info when you load up on taget pc it will save there path re wright the file with updated path

所以我的电脑 

so my pc 

Karaoke Bad Manners-我的女孩lollopop No DatacC:\ Users \ELFENLIEDTOPFAN5 \Desktop\KARAOKE \ KARAOKE \SCR JIM \ Karaoke Bad Manners-我的女孩lollopop.avi

Karaoke Bad Manners- My girl lollopopNo DatacC:\Users\ELFENLIEDTOPFAN5\Desktop\KARAOKE\KARAOKE\SCR JIM\Karaoke Bad Manners- My girl lollopop.avi

目标pc我希望程序做的是以下 

target pc what i want the program to do is the following 

Karaoke Bad Manners-我的女孩lollopop No DatacC:\ Users\Andy\Desktop\KARAOKE \ KARAOKE \SCR JIM \ Karaoke Bad Manners-我的女孩lollopop.avi

Karaoke Bad Manners- My girl lollopopNo DatacC:\Users\Andy\Desktop\KARAOKE\KARAOKE\SCR JIM\Karaoke Bad Manners- My girl lollopop.avi

将用户名更改为pc的用户名是可能的或改变整个路径。

change the username to the username of the pc is that possible or to change the whole path.

< /跨度>

推荐答案

Hello elfenliedtopfan2,

Hello elfenliedtopfan2,

如果要解析文本文件,专业的方法是保存数据作为XML格式或Json格式。至于上面的例子,它没有统一的格式,可能会导致一系列不匹配问题。我强烈建议你
将数据保存为XML格式,并通过XmlDocument对象解析文件。一个简单的例子如下所示。

If you want to parse the text file, the professional way is that save the data as XML format or Json format. As for the above example, it doesn't have a uniform format and maybe it will cause a series of mismatch issues. I strongly recommend that you save data as XML format and parse the file by XmlDocument object. A simple example as below.

对于给定的XML文件。

For a given XML file.

<?xml version="1.0" encoding="utf-8" ?>
<Songs>
  <Song>
    <Name>Name1</Name>
    <Singer>Singer1</Singer>
    <Path>Path1</Path>
  </Song>
  <Song>
    <Name>Name2</Name>
    <Singer>Singer2</Singer>
    <Path>Path2</Path>
  </Song>
</Songs>




解析如下。

 XmlDocument doc = new XmlDocument();
            doc.Load("../../XMLFile1.xml");

            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                foreach (XmlNode no in node) {
                    Console.WriteLine( no.InnerText);
                }
                Console.WriteLine("---------------");
            }

关于第二个问题,可以通过以下代码访问更新的路径,具体取决于不同的PC。

As for your second question, the updated path depend on different PC could be accessed by below code.

 string ss1 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Or

string ss2= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

致以最诚挚的问候,

Neil Hu


这篇关于解析文本文件并仅保存歌曲名称和文件路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆