In the app store of Win10, Himalayas can be installed and interested audio files can be downloaded. The downloaded audio is shown in the following figure. There are two json files (Figure 1). One of the json files (2677885list.json) contains the details of the downloaded audio, including id and title (the real name of the file); the audio file is a string of pure digital IDs as the name, without showing the real name of the audio (Figure 2). You can use this id to find the real name of the audio file and rename the audio file. The effect of renaming the audio file (Figure 3).
I used WinForm to develop an application. The interface consists of three parts: 1. Select audio directory; 2. Select json file; 3. Start.
1. Select audio Directory:
1 private void bt_select_file_Click(object sender, EventArgs e) 2 { 3 FolderBrowserDialog path = new FolderBrowserDialog(); 4 path.ShowDialog(); 5 tb_file.Text = path.SelectedPath; 6 }
2. Select the json file:
1 private void bt_select_json_Click(object sender, EventArgs e) 2 { 3 OpenFileDialog file = new OpenFileDialog(); 4 file.ShowDialog(); 5 tb_json.Text = file.FileName; 6 }
3. start:
(1) define FileInfo entity class
1 public class FileInfo 2 { 3 public string id { get; set;} 4 public string title { get; set; } 5 }
(2) define the generic class object of FileInfo, read the json file, convert it to string type, and then deserialize it to class object
1 // Definition FileInfo Generic class object for 2 List<FileInfo> fileInfo = new List<FileInfo>(); 3 4 // read json Files, converting to string Type, deserialized as class object 5 using (StreamReader file = File.OpenText(tb_json.Text)) 6 { 7 using (JsonTextReader reader = new JsonTextReader(file)) 8 { 9 JToken token = JToken.ReadFrom(reader); 10 string json = token.ToString(); 11 fileInfo = JsonConvert.DeserializeObject<List<FileInfo>>(json); 12 } 13 }
(3) get the audio file in the directory
1 // Get audio files in directory 2 DirectoryInfo dircetoryInfo = new DirectoryInfo(tb_file.Text); 3 System.IO.FileInfo[] files = dircetoryInfo.GetFiles();
(4) find the corresponding file according to the id information in json, then use title and. mp3 to form a new file name, and finally use MoveTo method to rename it
1 // according to json Medium id Information to find the corresponding file, and then use title and.mp3 Make up a new filename, and use the MoveTo Method to rename 2 foreach (var file in files) 3 { 4 string[] sArray = file.Name.Split('.'); //Get file name without extension 5 foreach (var item in fileInfo) 6 { 7 if (sArray[0] == item.id) 8 { 9 string destPath = Path.Combine(tb_file.Text, item.title + ".mp3"); //Combine new file name and original path 10 file.MoveTo(destPath); // File rename 11 } 12 } 13 }