C# Rename Folder on remote Computer, prompt user for computer name

Hi guys
I have a small program that renames a folder, you can see the code below.
There is currently two Text.boxes, one ask for the file you want renamed and the other the name you want it renamed to.  It works but only on local computer without asking wich computer you want to rename the file on.
I created a third Textbox that i want to prompt user for a computer to rename the folder.
Can you please help me with this bit of code.  Its domain environment and i have full admin rights so it should use current account.

string EnterComputer = TextBox3.Text;

Open in new window


Full code.

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.IO;
 using System.Diagnostics;

public partial class Default2 : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

}
 protected void Button1_Click(object sender, EventArgs e)
 {
 
 string strsorc = TextBox1.Text;
 string strdest = TextBox2.Text;
 string EnterComputer = TextBox3.Text;
 try
 {
 
 DirectoryInfo sourcefile = new DirectoryInfo(strsorc);
 if (sourcefile.Exists)
 {
 sourcefile.MoveTo(strdest);
 Label3.Text = "File has been successfully renamed";
 }
 else
 {
 Label3.Text = "file not found";
 }
 }
 catch (Exception ex)
 {
 Label3.Text = ex.Message;
 }
 }
 }

Open in new window

kentrg11Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You'll want to use a UNC path for your remote accesses. This will take the form:  \\computerName\accessible\path. In short, it's the same as what your doing now, just instead of a (say) "C:\" you have a "\\machine_name\".
0
kentrg11Author Commented:
the computer name will have to be entered by user in Textbox and stored in a variable?
i guess i need help with the actual syntax.


\\variable1\accessible\path is this right?
0
kentrg11Author Commented:
ive been searching online for actual syntax but cant find anything on renaming folder on remote computers :(
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

käµfm³d 👽Commented:
Yeah, the computer name can be in a variable. Then you just need string concatenation.

e.g.

string computerName = TextBox3.Text;
string uncPath = @"\\" + computerName + @"\path\on\remote\computer";

Open in new window


And of course you will need a rename function:

string oldFilename = uncPath + "oldFilename.txt";
string newFilename = uncPath + "newFilename.txt";

System.IO.File.Move(oldFilename, newFilename);

Open in new window

0
kentrg11Author Commented:
thanks kaufmed
Im actually trying to move a folder not a file, ive made some adjustments and im getting the following error.  
"Source and destination path must have identical roots. Move will not work across volumes."
I just want to rename the Username folder to Username.old

here is the code below
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.IO;
 using System.Diagnostics;

public partial class Default2 : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

}
 protected void Button1_Click(object sender, EventArgs e)
 {
     string UserName = TextBox1.Text;   //prompt for username
     string computerName = TextBox2.Text;  //promot for pc name
     string uncPath = @"\\" + computerName + @"\c$\users" + UserName;   //set unc path to \\computerName\c$\users\Username
     string old = "old";  //declate old

try
 {
 System.IO.Directory.Move(uncPath, old);  //rename the folder to old
 Label3.Text = "Folder has been successfully renamed";
 
 {
 Label3.Text = "Folder not found";
 }
 }
 catch (Exception ex)
 {
 Label3.Text = ex.Message;
 }
 }
 }

Open in new window

0
käµfm³d 👽Commented:
This is because your old variable does not specify the absolute network (a.k.a. UNC) path. It simply says "old", which tells the Move method that you expect to move it to a folder relative to your system's working directory, which if you didn't change it will be the directory where your executable resides (typcially the "bin" directory during development). You need to fill out that part of your path.

e.g.

string old = uncPath + "old";

Open in new window


P.S.

This is what the "Move will not work across volumes" is referring to:  trying to move a folder from a network location to a local location (or vice versa).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kentrg11Author Commented:
excellent help, thank you its now working
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.