debugging 即使在手动键入文件路径后仍出现“找不到文件”异常

brc7rcf0  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(142)

我尝试使用方法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"); }
        }

`

holgip5t

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.

相关问题