Get machine name path from relative or absolute path

dotnet framework

    Next

  • 1. .NET Printing and Windows 2003 Server VisibleClippingBounds proble
    I have a DOTNet printing application I wrote and also a Crystal Reports printing issue on a Windows 2003 Server. The visible clipping area bounds returned is incorrect and causes the printout to be clipped to this incorrect smaller region (about 33% of the size it should be). The code below is from the onPagePrint method protected override void OnPrintPage(PrintPageEventArgs e) { // Draw a box around VisibleClipBounds Graphics gx = e.Graphics; g.DrawRectangle( Pens.Black, Rectangle.Truncate gx.VisibleClipBounds)); ..... ..... } The properties returned are incorrect on a Windows 2003 Server and correct on Windows XP. i.e The gx.VisibleClipBounds.Height is 1066.667 on Windows XP and 416.3333 on Windows 2003 Server. The printer is an HP LaserJet 5M. I have not tested any other printers as of yet. This incorrect value causes my program and Crystal Reports to have the report clipped to this bounds and thus cutoff. Any ideas or is this a bug? Thanks Bob Maier
  • 2. what's better Static Methods or Instace Methods
    My AIM is the performance, so what is better to use, static method or instance method? and why? Thanks a lot
  • 3. remoting and file transfer
    Hello, I'm trying to learn about file transfer using remoting, and I was wondering if there is some sample code that could help me understand it. I've tried google, but almost all examples i've seen use sockets directly. any help is appreciated. thanks!
  • 4. dotnet installation
    Hi, I try to install donNet on XP Pro but I can not pass all prerequisites stage. I have problem with installing .Net Framework 1.1 and J# .Net Redistributable Package 1.1 Ttied both in package installation or by individual setup I also download Framework from MS site but that installation also crush. Checked that IE contains SP1. Any ideas? Thanks

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 13 guest