7,765
社区成员
发帖
与我相关
我的任务
分享
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace 修改小说文件名
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MIOpen_Click(object sender, RoutedEventArgs e)
{
//获取文件地址
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "选择文件";
ofd.Multiselect = true;
ofd.Filter = "文本文档|*.txt";
if (ofd.ShowDialog() == false)
{
return;
}
List<string> NovelList = new List<string>();
foreach (string File in ofd.FileNames)
{
NovelList.Add(File);
NameChanged(File);
}
NovelList.Add("重命名完成");
LNovels.ItemsSource = NovelList;
}
static void NameChanged(String FileName)
{
String[] line = new String[5];
string NewName;
//获取文件名
using (FileStream fileStream = File.OpenRead(FileName))
{
using (StreamReader NovelName = new StreamReader(fileStream, Encoding.Default))//StreamReader读取中文时出现乱码
{
for (int i = 0; i < 5; i++)
{
line[i] = NovelName.ReadLine();
}
}
}
//重命名
DirectoryInfo di = Directory.GetParent(FileName); //获取父系文件夹名
if (line[0].Contains("笔下文学"))
{
NewName = di.ToString() + "\\" + line[3].Substring(1, line[3].Length - 2) + ".txt";//生成新文件名
}
else if (line[4].Contains("http://www.qingkan520.com/"))
{
NewName = di.ToString() + "\\" + line[0].Substring(1, line[0].Length - 2) + ".txt";
}
else
{
NewName = FileName;
}
File.Move(FileName, NewName);
}
}
}