2012-04-02 157 views
1

好吧,所以我有这两个代码都工作,但我不能为我的生活弄清楚如何让他们两个一起工作。我希望能够从第一段代码中获得最终结果,它告诉我用户的绝对桌面路径(即时通讯也在讨论也来自域的用户,因为程序在没有第一个代码的情况下工作就可以找到如果用户是基于用户的桌面下车即到实际机器本身上的帐户,但如果你登录上一域的帐户这就是它不工作。这里是我的代码,我把复制文件获取绝对桌面路径

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

这里是一个桌面的绝对路径的代码(我认为也是,如果用户从一个域网络登​​录工作。)

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

再次澄清我的问题。我有这两段代码。我的原始程序只有在用户帐户位于实际的计算机硬盘驱动器上时才起作用,但是,即使用户位于域组中,帐户信息是第二段代码也应该找到用户桌面的目录从远程位置的服务器检索并且计算机登录到该服务器,然后将信息保存回远程服务器)。我想将这两者结合起来,以便它可以在本地磁盘上的用户以及来自域组的用户上运行。与我一起工作的计算机只是窗口。

+0

根本没有帮助...... – 2012-04-02 18:24:02

+0

这只是告诉你,你应该缩短你的问题,只保留与你的问题相关的代码。 – talnicolas 2012-04-02 18:32:46

+0

你仍然明白我需要什么,但我知道它不是最好的方式来提问,但是我在寻找答案,而不是问我问题的方法,我知道我没有以最好的形式提问,但我是匆忙。发表评论会将其从特色问题中删除,在这些问题中,实际上能够帮助我的人可以看到,但现在已从其中移除,并且仅限于看到它的人,并且他们可能会选择不回答。所以,如果你要发表评论,至少应该包括一些可以帮助我解决问题的代码。谢谢。 – 2012-04-02 18:36:19

回答

1

如果你想获得方法getCurrentUserDesktopPath()的结果在你的移动器类,你只需要把这一行你的主要方法:

String desktopPath = WindowsUtils.getCurrentUserDesktopPath(); 

由于这种方法是由静态你不需要申报WindowsUtils对象。