Reputation: 147
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();
}
}
}
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.
Upvotes: 1
Views: 5372
Reputation: 14053
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();
As this method is made static you don't need to declare a WindowsUtils
object.
Upvotes: 1