Get machine name path from relative or absolute path

E

elgdav

Hello, is it possible in C#.Net to get a path to a file in the form
"\\Spain\Madrid\readme.txt" from a relative path i.e "readme.txt" or
from a absolute path in the following form "C:\Madrid\readme.txt" where
"Spain" is the name of the machine that the file is on? Thanks in
advance.
 
C

Cowboy \(Gregory A. Beamer\)

The path you are looking for is a UNC path.

You can get the machine name or IP
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4549&lngWId=10

and then concatenate the rest of the UNC path by querying directory
structure/path (MapPath, for example) for a file name.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
E

elgdav

Thanks for the reply Cowboy, I downloaded the code at the link you
suggested by that simply does Dns.GetHostName() which will return the
MAchine name of the machine the code is running on but i want the
machine name of the machine a file is stored on. Sorry my example used
the C drive and so your above solution would work for that but what if
the drive was a mapped network drive and so the file was not on the
same machine, how would i get the machine name then?

Thanks for your help.
 
D

Damien

elgdav said:
Hello, is it possible in C#.Net to get a path to a file in the form
"\\Spain\Madrid\readme.txt" from a relative path i.e "readme.txt" or
from a absolute path in the following form "C:\Madrid\readme.txt" where
"Spain" is the name of the machine that the file is on? Thanks in
advance.

I don't think you're going to find anything simply baked into
..Net/Win32 to do this.

Think of the following file:

C:\A\B\C\D\Blah.txt

File ACLs are set to Everyone/Full Control. D is shared and has
security set such that only Domain Administrators can access the share.
C is shared and has security set such that Domain Users can access it.
B is shared such that Everyone can access it.

Having set the scene, imagine that you want to get a UNC path to
C:\A\B\C\D\Blah.txt. What should your imagined function return? Any of
\\machine\B\C\D\Blah.txt, \\machine\C\D\Blah.txt or
\\machine\D\Blah.txt may be the correct answer. Which is correct is
something the system cannot determine.

Of course, the above model is hopelessly flawed from a security
perspective, since if people are aware of the hierarchy, they can
always access via B, but it was to demonstrate that multiple shares
with alternate security permissions may exist, and the correct decision
is not something that the system can infer.

Damien
 
E

elgdav

Heres a workaround i coded if anyone else ever gets this problem, i
takes an absolute path and uses Net.exe to get the machine name, to
which you can append the rest of the path.


private string GetUncPath(string absolutePath)
{
int colonIndex = absolutePath.IndexOf(":");

if (colonIndex != 1)
{
throw new Exception("Invalid Path <" + absolutePath +
">.");
}

System.Diagnostics.Process net = new
System.Diagnostics.Process();
net.StartInfo.FileName = "Net.exe";
net.StartInfo.UseShellExecute = false;
net.StartInfo.RedirectStandardOutput = true;
net.StartInfo.Arguments = "use " +
absolutePath.Substring(0, colonIndex + 1);
net.Start();
string line = "";
string RemoteName = "Remote name";
string machineName = "";

while ((line = net.StandardOutput.ReadLine()) != null &&
machineName.Length == 0)
{
if (line.StartsWith(RemoteName))
{
machineName = line.Remove(0,
RemoteName.Length).Trim();
}
}

return machineName + absolutePath.Substring(colonIndex + 1,
absolutePath.Length - (colonIndex + 1));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top