获取绝对桌面路径 [英] Get absolute desktop path

查看:82
本文介绍了获取绝对桌面路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有这两段代码都可以工作,但是我一生无法弄清楚如何让它们一起工作.我希望能够从第一段代码中获得最终结果,该结果告诉我用户的绝对桌面路径(即谈论来自域的用户,因为该程序无需第一个代码即可找到工作域)用户桌面(如果用户基于实际计算机上的帐户),但是如果登录的是域上的帐户则无法使用,这是我用来复制文件的代码

Ok, so I have these two pieces of code that both work, but i cant for the life of me figure out how to get both of them to work together. I want to be able to get the end result from the first piece of code, that tells me the absolute desktop path of the user (im talking about users that are also from domains, because the program does work without the first code to find the user desktop if the user is based off an account that is on the actual machine itself, but it doesnt work if you log in on an account thats on a domain. here is my code that i made to copy files

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

 public class Mover
     public static void main(String[] args) throws IOException, InterruptedException    {   

        String currentdir = new File(".").getAbsolutePath();
        File TS3S = new File(currentdir + "/Teamspeak 3");
        File TS3D = new File(currentdir + "/TS3");
        File MinecraftLauncherS = new File(currentdir + "/Minecraft");
        File MinecraftLauncherD = new File(currentdir + "/Desktop");
        File ShortcutS = new File(currentdir + "/Shortcuts");
        File ShortcutD = new File(currentdir + "/Desktop");
        File MinecraftFilesS = new File(currentdir + "/minecraft files");
        File MinecraftFilesD = new File(currentdir + "/Application Data");
        File FrapsS = new File(currentdir + "/Fraps");
        File FrapsD = new File(currentdir + "/Fraps");

        //make sure source exists
        if(!TS3S.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(TS3S,TS3D);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(MinecraftLauncherS,MinecraftLauncherD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }


        //make sure source exists
        if(!MinecraftFilesS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(MinecraftFilesS,MinecraftFilesD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!ShortcutS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(ShortcutS,ShortcutD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(FrapsS,FrapsD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
        Runtime.getRuntime ().exec (currentdir + "/Desktop/TS3/ts3client_win32.exe");
        Runtime.getRuntime ().exec (currentdir + "/Desktop/Minecraft.exe");
        System.exit(0);
        }

    public static void copyFolder(File src, File dest)
        throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                   out.write(buffer, 0, length);
                }

                in.close();
                out.close();
                System.out.println("File copied from " + src + " to " + dest);
        }
    }
}

这是获取桌面的绝对路径的代码(如果用户从域网络登录,我认为这也是可行的)

And here is the code that gets the absolute path of the desktop (which i think also works if the user is logged in from a domain network.)

import java.io.*;

public class WindowsUtils {
  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL 
 + "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
 + "Explorer\\Shell Folders\" /v DESKTOP";

  private WindowsUtils() {}

  public static String getCurrentUserDesktopPath() {
try {
  Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
  StreamReader reader = new StreamReader(process.getInputStream());

  reader.start();
  process.waitFor();
  reader.join();
  String result = reader.getResult();
  int p = result.indexOf(REGSTR_TOKEN);

  if (p == -1) return null;
  return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
  return null;
}
  }

  /**
   * TEST
   */
  public static void main(String[] args) {
    System.out.println("Desktop directory : " 
       + getCurrentUserDesktopPath());
  }


  static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;

StreamReader(InputStream is) {
  this.is = is;
  sw = new StringWriter();
}

public void run() {
  try {
    int c;
    while ((c = is.read()) != -1)
      sw.write(c);
    }
    catch (IOException e) { ; }
  }

String getResult() {
  return sw.toString();
}
  }
}

再次

澄清我的问题.我有这两段代码.仅当用户帐户位于其自身的实际计算机硬盘驱动器上时,我的原始程序才起作用,但是,即使用户位于域组中,第二段代码也应查找用户桌面的目录(帐户信息为从远程位置的服务器中检索到该计算机,然后计算机登录到该服务器,然后将信息保存回远程服务器上.我想将两者结合起来,以便它既可以在本地磁盘内部的用户上使用,也可以在域组中的用户上使用.我正在使用的计算机只是Windows.

again to clarify my question. I have those two pieces of code. My original program works only if the user account is on the actual computer hard drive its self, but, the second piece of code is supposed to find the directory of the users desktop even if the user is inside of a domain group (account information is retrieved from a server in a remote location and the computer logs onto it, and then saves information back on the remote server). I want to combine those two so that it works on both users inside the local disk, and on users that are from a domain group. the computers im working with are only windows.

推荐答案

如果要在Mover类中获取方法getCurrentUserDesktopPath()的结果,只需在主方法中添加以下行:

If you want to get the result of the method getCurrentUserDesktopPath() in your Mover class, you just need to put this line in your main method:

String desktopPath = WindowsUtils.getCurrentUserDesktopPath();

由于此方法是静态的,因此无需声明WindowsUtils对象.

As this method is made static you don't need to declare a WindowsUtils object.

这篇关于获取绝对桌面路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆