我正在Microsoft Visual C#2010上使用C#开发Windows应用程序。
该应用程序旨在帮助用户一次更改目录中多个文件的名称。例如:
我在目录c:/ temp中有五个文件:
rainmoon.txt
terrain.txt
things.txt
pipe.txt
TEMP.TXT
如果我尝试重命名“rainmoon.txt”来说“rainsun.txt”,那么我只需输入我只想用“太阳”代替“月亮”。我的应用程序是这样的,它将浏览包含“月亮”的所有文件,然后继续用“sun”替换它
当文件所在的文件名(temp.txt)和文件夹(也称为temp)具有相同名称时,问题就出现了。因此,对于上面给出的示例,如果我尝试重命名“temp.txt”的甚至部分,我会收到DirectoryNotFoundException。
以下是我使用的代码。我已经尽力解决这个问题,我强烈认为这是由于File.Move。
for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
{
globalvar.fName = filesCheckList.CheckedItems[x].ToString();
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);
FileInfo[] f = mydir.GetFiles();
foreach (FileInfo file in filesx)
{
//try
{
if (file.Name == globalvar.fName)
{
if (file.Name != "History.txt")
File.Move(file.FullName, file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP));
}
}
}
}
我的完整代码如下,以防有人想看看。我不排除可能在编码中的某个地方导致了这个问题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TOMOD_W_A
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
applyButton.Enabled = false;
}
private void applyButton_Click(object sender, EventArgs e)
{
globalvar.FIL = filTxt.Text;
globalvar.REP = repTxt.Text;
if (string.IsNullOrWhiteSpace(dirTxt.Text))
MessageBox.Show("Please Key in a Directory");
else if (globalvar.FIL == String.Empty)
MessageBox.Show("Please give a file name");
else
{
try
{
if (filesCheckList.CheckedItems.Count != 0)
{
for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
{
globalvar.fName = filesCheckList.CheckedItems[x].ToString();
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
//DirectoryInfo[] directories = mydir.GetDirectories(".", SearchOption.TopDirectoryOnly);
FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);
FileInfo[] f = mydir.GetFiles();
foreach (FileInfo file in filesx)
{
//try
{
if (file.Name == globalvar.fName)
{
if (file.Name != "History.txt")
File.Move(file.FullName, file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP));
}
}
/*****************************
globalvar.DIR = dirTxt.Text;
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);
FileInfo[] f = mydir.GetFiles();
filesCheckList.Items.Clear();
foreach (FileInfo file in filesx)
{
if (file.Name != "History.txt")
filesCheckList.Items.Add(file.Name);
}
* */
/* catch (IOException)
{
MessageBox.Show("File names cannot be the same in the same folder");
MessageBox.Show("File name cannot be empty");
}
catch (ArgumentException)
{
MessageBox.Show("Cannot contain symbols / \\ : * ? < > | \" \n File name cannot be con");
}*/
}
}
}
}
catch (ArgumentNullException)
{
MessageBox.Show("Please key in a directory");
}
//if (File.Exists(globalvar.DIR + @"\History.txt"))
try
{
string content = File.ReadAllText(globalvar.DIR + @"\History.txt");
File.WriteAllText(globalvar.DIR + @"\History.txt", String.Empty);
/* using (System.IO.StreamWriter files = new System.IO.StreamWriter(globalvar.DIR + @"\History.txt", true))
{
files.WriteLine("For multiple files:");
files.WriteLine("Changed " + "\"" + globalvar.FIL + "\"" + " to " + "\"" + globalvar.REP + "\"");
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
FileInfo[] f = mydir.GetFiles();
globalvar.clear = 1;
foreach (FileInfo file in f)
{
if (file.Name != "History.txt")
files.WriteLine(file.Name);
}
for (int x = 0; x <= filesCheckList.CheckedItems.Count - 1; x++)
{
files.WriteLine("in " + filesCheckList.CheckedItems[x].ToString() + "\n");
}
files.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
files.WriteLine("\n");
files.WriteLine(content);
} */
}
catch (FileNotFoundException)
{
using (System.IO.StreamWriter files = File.CreateText(globalvar.DIR + @"\History.txt"))
{
files.WriteLine("For multiple files:");
files.WriteLine("Changed " + "\"" + globalvar.FIL + "\"" + " to " + "\"" + globalvar.REP + "\"");
files.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
files.WriteLine("\n");
}
}
MessageBox.Show("Your selected files have been changed");
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
public static class globalvar
{
public static string DIR;
public static string FIL;
public static string REP;
public static string OPT;
public static string fName;
public static int clear = 0;
public static int proceed = 0;
public static int filpro = 0;
public static int cignm = 0;
public static int mngic = 0;
}
private void browseButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
dirTxt.Text = folderBrowserDialog1.SelectedPath;
}
private void shfilButton_Click(object sender, EventArgs e)
{
applyButton.Enabled = true;
try
{
globalvar.DIR = dirTxt.Text;
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
FileInfo[] filesx = mydir.GetFiles(".", SearchOption.AllDirectories);
FileInfo[] f = mydir.GetFiles();
filesCheckList.Items.Clear();
foreach (FileInfo file in filesx)
{
if (file.Name != "History.txt")
filesCheckList.Items.Add(file.Name);
}
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Directory not found");
}
catch (ArgumentException)
{
MessageBox.Show("Directory not found");
}
catch (IOException)
{
MessageBox.Show("Directory is invalid");
}
}
private void filChk_CheckedChanged(object sender, EventArgs e)
{
if (filChk.Checked)
{
for (int i = 0; i < filesCheckList.Items.Count; i++)
{
filesCheckList.SetItemChecked(i, true);
}
}
else
{
for (int i = 0; i < filesCheckList.Items.Count; i++)
{
filesCheckList.SetItemChecked(i, false);
}
}
}
private void dirTxt_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
globalvar.DIR = dirTxt.Text;
DirectoryInfo mydir = new DirectoryInfo(globalvar.DIR);
FileInfo[] f = mydir.GetFiles();
globalvar.clear = 1;
filesCheckList.Items.Clear();
foreach (FileInfo file in f)
{
if (file.Name != "History.txt")
filesCheckList.Items.Add(file.Name);
}
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Directory not found");
}
catch (ArgumentException)
{
MessageBox.Show("Directory not found");
}
catch (IOException)
{
MessageBox.Show("Directory is invalid");
}
}
}
private void dirTxt_TextChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
我强烈认为这是由于File.Move。
旧引文:&#34;如果你认为.net或JVM坏了,那就该休息了#34;
此外,您的代码并不漂亮,我绝对会建议您阅读此类书籍:http://www.amazon.fr/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
但是现在让我们忘记这一点。
请在此行中加上一个断点:
File.Move(file.FullName, file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP));
现在使用您的文件和文件夹具有相同名称的示例运行您的代码。 你看到了什么?
是的,file.FullName
实际上包含文件夹的名称和文件的名称。现在如果你这样做会发生什么:
file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP))
您将重命名文件和文件夹。
例如,使用初始文件: C:/temp/temp.txt
用太阳替换temp,你得到c:/sun/sun.txt
当您需要首先创建文件夹时,.NET会说&#34; c:/ sun&#34;不存在。
据我了解你的用例,你想要的只是文件名:
file.Name.Replace(globalvar.FIL, globalvar.REP))
所以你应该记录文件夹名称,只需重命名文件并将其移动到文件夹+ newfileName。
希望它有所帮助。
答案 1 :(得分:0)
问题在那里
file.FullName.ToString().Replace(globalvar.FIL, globalvar.REP)
file.FullName
是完整路径,如C:\ Temp \ Temp.txt,所以当你替换字符串&#39; Temp&#39;由&#39; abc&#39;你有C:\ abc \ abc.txt。但是没有这样的文件夹&#39; abc&#39;
你应该结合这样的新路径:
file.Directory.FullName.ToString() + file.Name.ToString().Replace(globalvar.FIL, globalvar.REP)