Answer the question
In order to leave comments, you need to log in
How to move a file?
I have two methods: search and sort.
Search:
public void Search()
{
title = title_tb.Text;
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = "fb2";
ofd.Filter = "fb2 files (*.fb2)|*.fb2|All files (*.*)|*.*";
Nullable<bool> result = ofd.ShowDialog();
if (result.HasValue == true)
{
// Open document
filename = ofd.FileName;
title_tb.Text = System.IO.Path.GetFileName(filename);
}
}
private void SortClick_btn(object sender, RoutedEventArgs e)
{
//for sorting pb2
title = title_tb.Text;
path = Link_tb.Text;
subpath = author_tb.Text;
seriespath = SeriesOfBooks_tb.Text;
string mydir = [email protected]"{path}\{subpath}\{seriespath}";
if (!Directory.Exists(mydir)) Directory.CreateDirectory(mydir);
if (!string.IsNullOrWhiteSpace(SeriesOfBooks_tb.Text))
// **переместить в папку seriespath**
}
Answer the question
In order to leave comments, you need to log in
If I understand correctly that the initial path is in title and the final path is in mydir, then:
I would add another check for the existence of a file in the specified path, because if the file already exists, an IOException will be thrown. Eventually:
if (File.Exists(mydir))
File.Delete(mydir);
File.Move(title, mydir);
or use the move command line program.
That is, just create a child process without creating a window. Launch options "move c:\test\file1.txt D:\folder2\file2.txt" - move the file file1.txt from the test directory of the C: drive to the folder2 directory of the D: drive under the name file2.txt.
I think this is a normal solution, because. the task of moving the file will be assigned to the program that is provided with the operating system. And in your program, you already simply implement search and sorting algorithms.
something like this
void yourbestfunction()
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "move C:\test\file1.txt D:\folder2\file2.txt";
Process.Start(startInfo);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question