Get machine name path from relative or absolute path

dotnet framework

    Next

  • 1. doc. inconsistency: default p/invoke marshaling for strings
    Below given several excerpts from msdn library documentation for default p/invoke marshaling for strings. which of them says the truth? which one should we depend on? thanks --- Default Marshaling for Strings ms-help://MS.MSDNQTR.2003APR.1033/cpguide/html/cpcondefaultmarshalingforstri ngs.htm Strings Used in Platform Invoke UnmanagedType.BStr (default) --- MarshalAsAttribute Class ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfSystemRuntimeInteropServic esMarshalAsAttributeClassTopic.htm By default, the runtime marshals a string parameter (and fields in a value type) as a LPStr to a platform invoke method or function. For additional information, see Default Marshaling Behavior. --- Platform Invoke Tutorial ms-help://MS.MSDNQTR.2003APR.1033/csref/html/vcwlkPlatformInvokeTutorial.htm #pinvoke_defaultmarshaling For example, the default marshaling for C# string values is to the type LPTSTR (pointer to TCHAR char buffer).
  • 2. Create help files/Online help in .NET
    Hello, I would like to know how we can create help files or online help for any application in .NET. Also could someone tell me briefly whats exactly Microsoft Help v.1.3 or v.2.0? Thanks in advance. PK
  • 3. resolving own IP address
    Hi: I'm writing a C# server application that needs to display its ip address. I've used System.Net.Dns.Resolve("localhost") as the call and iterated through the results, but it just gives me back the loopback address 127.0.0.1. I have been looking all around for advice online on how to resolve ones own IP address, but have found nothing. Could anyone give me some pointers on how this might be accomplished? Thanks in advance, Steve

Get machine name path from relative or absolute path

Postby elgdav » Sat, 02 Sep 2006 21:40:10 GMT

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.


Re: Get machine name path from relative or absolute path

Postby Cowboy (Gregory A. Beamer) » Sat, 02 Sep 2006 22:00:14 GMT

The path you are looking for is a UNC path.

You can get the machine name or IP
 http://www.**--****.com/ 

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!
*************************************************







Re: Get machine name path from relative or absolute path

Postby elgdav » Sat, 02 Sep 2006 22:19:13 GMT

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.










Re: Get machine name path from relative or absolute path

Postby Damien » Sat, 02 Sep 2006 23:31:51 GMT



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


Re: Get machine name path from relative or absolute path

Postby elgdav » Wed, 06 Sep 2006 23:58:18 GMT

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


Similar Threads:

1.why relative hint path but absolute reference path

I have a vs.net 2002 solution that contains a number of projects
All projects are created in folders under the solution folder.
The idea here is that this entire solution should be 'moveable' ie. copied
to and useable from a new directory or computer.
And it almost is.

One project makes reference to an existing .net dll that also exists in a
folder under the solution folder.
The project file shows a relative hint path to the dll ie
"..\..\dllFolder\my.dll"
If I move the solution to a new folder and the open solution from the new
location, the dll can no longer be found.  if i restore the original
solution folder, the dll is once again found.  so obviously, although the
project file includes a relative hint path, vs.net is in fact looking for
the dll at an absolute location.  This is causing headaches as this solution
cannot be 'passed around'  without re-adding the reference to this dll.

is there some setting somewhere to get the relative path used ?
there does not seem to be any problem using relative project references - is
there some trick using a dummy project to get this to work ?

gerry



2.absolute path to relative path conversion

3.why relative hint path but absolute reference path

I have a vs.net 2002 solution that contains a number of projects
All projects are created in folders under the solution folder.
The idea here is that this entire solution should be 'moveable' ie. copied
to and useable from a new directory or computer.
And it almost is.

One project makes reference to an existing .net dll that also exists in a
folder under the solution folder.
The project file shows a relative hint path to the dll ie
"..\..\dllFolder\my.dll"
If I move the solution to a new folder and the open solution from the new
location, the dll can no longer be found.  if i restore the original
solution folder, the dll is once again found.  so obviously, although the
project file includes a relative hint path, vs.net is in fact looking for
the dll at an absolute location.  This is causing headaches as this solution
cannot be 'passed around'  without re-adding the reference to this dll.

is there some setting somewhere to get the relative path used ?
there does not seem to be any problem using relative project references - is
there some trick using a dummy project to get this to work ?

gerry






4.Absolute or Relative path?

5.Relative & absolute path question (JavaScript, mostly)

6. User controls --> Relative/Absolute Paths

7. Absolute url from relative path

8. Absolute and relative paths in applicaiton



Return to dotnet framework

 

Who is online

Users browsing this forum: No registered users and 27 guest