如何使用Vala将文件移动到另一个目录?

问题描述 投票:1回答:1

我试图根据Vala documentation将文件移动到另一个目录,但以下方法不起作用。

请告诉我,我是否误解了移动方法的工作原理。

moveMyFile V1:

public void moveMyFile(){

    GLib.File dest = GLib.File.new_for_path("~/Desktop/dest/");
    GLib.File src = GLib.File.new_for_path("~/Desktop/src/test.txt");

    print("\ndest path : %s \n", dest.get_path());
    print("src path : %s \n", src.get_path());

    if(dest.query_exists() && src.query_exists()){
        try {
            src.move(dest, FileCopyFlags.NONE, null);
        } catch (Error e) {
            print ("moveMyFile Error: %s\n", e.message);
        }
    }

}

终端输出(没有错误,dest中没有test.txt):

dest path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/dest 
src path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/src/test.txt

------------------Update 1----------------------

moveMyFile V2:

public void moveMyFile(){

    GLib.File dest = GLib.File.new_for_path("~/Desktop/dest/test.txt");
    GLib.File src = GLib.File.new_for_path("~/Desktop/src/test.txt");

    print("\ndest path : %s \n", dest.get_path());
    print("src path : %s \n", src.get_path());

    if(dest.query_exists() && src.query_exists()){
        try {
            src.move(dest, FileCopyFlags.NONE, null);
        } catch (Error e) {
            print ("moveMyFile Error: %s\n", e.message);
        }
    }

}

终端输出(没有错误,dest中没有test.txt):

dest path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/dest/test.txt 
src path : /home/srdr/Serdar/Workspaces/WorkspaceBudgie/budgie-projects/budgie-trash/build/~/Desktop/src/test.txt

------------------Update 2----------------------

moveMyFile V3:

public void moveMyFile(){

string homePath = Environment.get_home_dir();
string destPath = homePath + "/Desktop/dest/test.txt";
string srcPath = homePath + "/Desktop/src/test.txt";

GLib.File dest = GLib.File.new_for_path(destPath);
GLib.File src = GLib.File.new_for_path(srcPath);

print("\ndest path : %s \n", dest.get_path());
print("src path : %s \n", src.get_path());

if(dest.query_exists() && src.query_exists()){
    try {
        src.move(dest, FileCopyFlags.NONE, null);
    } catch (Error e) {
        print ("moveMyFile Error: %s\n", e.message);
    }
}

}

终端输出(没有错误,dest中没有test.txt):

dest path : /home/srdr/Desktop/dest/test.txt 
src path : /home/srdr/Desktop/src/test.txt
file directory move glib vala
1个回答
2
投票

我正在检查dest文件是否存在。我从if语句中删除了dest.query_exists()。现在它正在运作。

 public void moveMyFile(){

        string homePath = Environment.get_home_dir();
        string destPath = homePath + "/Desktop/dest/test.txt";
        string srcPath = homePath + "/Desktop/src/test.txt";

        GLib.File dest = GLib.File.new_for_path(destPath);
        GLib.File src = GLib.File.new_for_path(srcPath);

        print("\ndest path : %s \n", dest.get_path());
        print("src path : %s \n", src.get_path());

        if(src.query_exists()){
            try {
                print ("moveMyFile try\n");
                src.move(dest, FileCopyFlags.NONE, null);
            } catch (Error e) {
                print ("moveMyFile Error: %s\n", e.message);
            }
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.