A
A
Anton_repr2019-12-18 15:09:05
WPF
Anton_repr, 2019-12-18 15:09:05

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);
            }
        }

I find a file of a certain format, in the future it needs to be moved to another folder.
Sorting:
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**

        }

If SeriesOfBooks_tb.Text is not empty, then the selected file is added to the folder with the name written in SeriesOfBooks_tb.Text. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Vlasov, 2019-12-18
@andreyvlv

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);

D
deyen, 2019-12-18
@deyen

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 question

Ask a Question

731 491 924 answers to any question