我尝试使用方法System.IO.File.Move(sourceFile,newFilePath);
来重命名文件,但我总是得到相同的错误消息,我的文件找不到。
我尝试手动写入源路径System.IO.File.Move(@"D:\Users\XXX\Desktop\TestOrHMoin",@"D:\Users\XXX\Desktop\TestOrHMoinNEW");
,但我仍然收到相同的错误消息。
这是我的完整代码
`
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
oldFilePath = openfiledialog.FileName;
listBox1.Items.Add(oldFilePath);
}
}
private void button2_Click(object sender, EventArgs e)
{
newFilePath = textBox1.Text;
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length);
newFilePath = oldFilePath + newFilePath;
string sourceFile = @oldFilePath;
string newFile = @newFilePath;
MessageBox.Show(sourceFile);
System.IO.File.Move(sourceFile,newFilePath);
// This part is the real code, the above Part is for debugging/testing
//System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
//if (fi.Exists)
//{
// fi.MoveTo(newFilePath);
// MessageBox.Show("Erfolgreich geändert");
//} else { MessageBox.Show("Abbruch"); }
}
`
1条答案
按热度按时间holgip5t1#
oldFilePath = oldFilePath.Remove(oldFilePath.Length-newFilePath.Length); was wrong
I needed to declare another variable, so oldFilePath gets untouched. It was my error. I still don't know, why the manuell direction got an error.