absolute path to relative path conversion

C#

    Next

  • 1. C# struct Constructors As Shown in IntelliSense? (M'soft Rep PLease)
    When a struct is created in C# and a parameterized constructor defined, the IntelliSense editor shows only the syntax for the parameterized constructor in tooltips, but not for the default constructor which appears to still exist. If this is not designed behavior, is it a bug or am I misusing my struct? If it is by design, could you please explain? I realize the behavior of a struct, as regards constructors, is different from that of classes in that a class will not retain an automatically defined default constructor if a parameterized constructor is defined by the programmer. The tooltip for struct constructor options seems to be following the rules for a class? I'm a bit confused, and any help getting straight would be much appreciated! -- Peter - [MVP - Academic]
  • 2. Missing property? Or a Blonde moment?
    I'm trying to write a plugin for visual studio, but I've hit a snag with a property that should be there, but kind of isn't. private bool IsProjectThemed( EnvDTE.Project project ) { try { if ( project.Globals.get_VariableExists( m_themedFlagName ) ) { return ( project.Globals.VariableValue( m_themedFlagName ) == true ); } else { return false; } } catch ( Exception ) { return false; } } But apparently... 'EnvDTE.Globals' does not contain a definition for 'VariableValue' But according to MSDN, it should be there. Any suggestions? Cheers, 4Space
  • 3. Localization Issues with Windows Form (.resx) file
    Hello, I need to know how can I generate a .resx file from code for different language culture. Basically the .resx file is in binary, is there anyway that I can generate a text file contains all the text for each controls on the form which then can be converted to a .resx file for a given culture. Basically I want to do what WINRES.EXE does but through code. Thanks,
  • 4. Refering to a type staticly
    Hi all, I am curious if there is a way to refer to a class type without having to create an instance of a type, and to GetType() on it. For instance, in Java, I could do something like Class classType = String.class; I assume that there is something similar in C#? Thanks, Dave.

absolute path to relative path conversion

Postby Rizaan Jappie » Fri, 14 Jan 2005 21:58:44 GMT

is it possible to get the relative path based on a absolute path in c#?

*** Sent via Developersdex  http://www.**--****.com/  ***
Don't just participate in USENET...get rewarded for it!

Re: absolute path to relative path conversion

Postby Rob Levine » Fri, 14 Jan 2005 22:38:53 GMT

Hi Rizaan,

I am not 100% sure what you mean by this question.
A relative path is relative to some other path.
Are you asking whether, given two absolute paths, you can automatically work 
out the relative path between them?

Rob








Re: absolute path to relative path conversion

Postby Rizaan Jappie » Fri, 14 Jan 2005 22:53:47 GMT

Sorry for being so vague Rob

what i need is the following :

If i have an absolute path to a file e.g.
c:\rizaan\pathsquestion\absolutepath\class1.cs

how would i go about getting the relative path in c# for class1.cs using
'c:\rizaan\pathsquestion\absolutepath\' as the source directory?

does this make sense at all?

*** Sent via Developersdex  http://www.**--****.com/  ***
Don't just participate in USENET...get rewarded for it!

Re: absolute path to relative path conversion

Postby Marek » Fri, 14 Jan 2005 23:03:15 GMT

If I understand what you want to do, when you want to do this in C# code,
setting Environment.CurrentDirectory and using only "relative paths" after
that would do what you expect

string path =
System.IO.Path.GetDirectoryName(@"c:\rizaan\pathsquestion\absolutepath\class
1.cs");
Environment.CurrentDirectory = path;

and you can use relative paths from here, unless CurrentDirectory is changed









Re: absolute path to relative path conversion

Postby Rob Levine » Fri, 14 Jan 2005 23:30:57 GMT

Hi Rizaan,

If I understand correctly, what you are trying to do is easy in the example 
you give as you are effectively just after the filename:

FileInfo fi = new FileInfo ( abPath ); // get a FileInfo for the file 
specified in the absolute path
string relPath = fi.Name; // the name property returns the unqualified name 
of the file

However, if the file you are after is in another directory then this is 
quite a lot harder. I can't think of a way off the top of my head without 
doing some sort of 'directory walk'.
You could walk from you absolute path to the root of your drive and then 
walk down the path to your file, building the relative path as you go along. 
This would work, but may not give you the most concise relative path 
possible.

I am still not 100% sure I have understood what you are trying to do though. 
Is it possible that the file you are after is in a different directory to 
the absolute path given? Are you trying to build a relative path between a 
directory and a file in another?

Apologies if it is obvious what you are saying and I am just missing the 
point !

Rob










Re: absolute path to relative path conversion

Postby Rizaan Jappie » Fri, 14 Jan 2005 23:48:59 GMT

what im trying to do is a bit hard to explain but here goes. I have been
assigned to write a program that creates VS.NET 2003 project files
(.csproj files) to a specified folder. If you look at the spec of the
.csproj file you will notice under the '<Files><Include><File>' section
that each file present in a particular project e.g. classes, forms etc
are all stored using a relative path e.g.  

<Files>
   <Include>
       <File
          RelPath = "class1.cs"
          Link = "..\..\..\..\..\CSharp\class1.cs"
          SubType = "Code"
          BuildAction = "Compile"
       />
   </Include>
</Files>

i have the absolute path of class1.cs but would like to get the relative
path of class1.cs so as to build the .csproj correctly...also i have
created an instance of FileInfo so i do have the properties of class1.cs
on hand...also note that the class1.cs file is linked to the project and
not always present in the same directory of the project.

thanks for the previous replies..

*** Sent via Developersdex  http://www.**--****.com/  ***
Don't just participate in USENET...get rewarded for it!

Re: absolute path to relative path conversion

Postby Rob Levine » Sat, 15 Jan 2005 00:30:28 GMT

Hi Rizaan,

Well - your problem makes sense but I can't think of a simple solution to be 
honest - sorry !
It seems to me that the best approach is something along these lines:

Take the absolute paths of your project file and your class file
e.g.
string projPath = "c:\dev\projects\myProj\myProj.csproj"
string classPath = "c:\dev\projects\myProj\src\blah\class1.cs"

Now examine these two paths and chop off the common part at the start of the 
path

projPath = "myProj.csproj"
classPath = "src\blah\class1.cs"

Now parse the longer of the paths, generating a relative path. e.g. every 
time you hit a \ symbol, add a .. to your rel path etc.
This should work but there are a number of caveats that spring to mind:
- will the two paths always be in the same format? ie file://c:\dev.... vs 
c:\dev....
- will the root be quoted in the same way? ie c:\dev... vs 
\\mymachine\c$\dev....
- will the slashes be the same way round ?
- etc

Hope this helps a bit, sorry for not having a more elegant solution.

Regards,

Rob
 http://www.**--****.com/ 








Re: absolute path to relative path conversion

Postby TWFyY2luIEdyesSZYnNraQ » Sat, 15 Jan 2005 02:39:55 GMT

Hi,

Some code for You:

         private static string EvaluateRelativePath(string mainDirPath
             , string absoluteFilePath) {
             string[] 
firstPathParts=mainDirPath.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
             string[] 
secondPathParts=absoluteFilePath.Trim(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);

             int sameCounter=0;
             for(int i=0; i<Math.Min(firstPathParts.Length, 
secondPathParts.Length); i++) {
                 if( 
!firstPathParts[i].ToLower().Equals(secondPathParts[i].ToLower()) ) {
                     break;
                 }
                 sameCounter++;
             }

             if( sameCounter==0 ) {
                 return absoluteFilePath;
             }

             string newPath=String.Empty;
             for(int i=sameCounter; i<firstPathParts.Length; i++) {
                 if( i>sameCounter ) {
                     newPath+=Path.DirectorySeparatorChar;
                 }
                 newPath+="..";
             }
             if( newPath.Length==0 ) {
                 newPath=".";
             }
             for(int i=sameCounter; i<secondPathParts.Length; i++) {
                 newPath+=Path.DirectorySeparatorChar;
                 newPath+=secondPathParts[i];
             }
             return newPath;
         }

HTH
Marcin

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.Get machine name path from relative or absolute path

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.

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.Generated files with attributes and absolute/relative paths

I have an attributed ATL project (call it B) that I am 
building with Microsoft Visual C++ .NET (55537-640-3684941-
18356).  In the project I include the COM generated file 
A.h (from another ATL project A).  The compiler interprets 
this and injects the command "import A.idl" into the idl 
for B.  So far everything is okay.  The problem is that 
when I build project B in debug, it references A.idl using 
the absolute path.  When I build project B in release, it 
references A.idl using relative addressing.  The case for 
release is a problem because I have other projects that 
want to include B.idl, but are in a different relative 
directory.  My general question is if there is 
documentation on how the compiler decides to use relative 
or absolute paths for the files it inserts.  Failing that, 
does anyone know why switching from debug build to release 
build would change the choice of absolute or relative 
paths?  (I could find no project setting related to this 
issue.)  Thanks for any input.

5.Absolute and relative paths in VC6 dsp files...

Hi,
In VC6.0 when i insert an IDL file into my project from a folder that is
different from my current project folder, i was expecting it to remember the
name of the path as i inserted it. E.G. If my project is in
c:\vcprojects\myapp\component1\component1.dsp and an IDL file is in
c:\IDL\common.idl
The .dsp file does a relative path include. The result looks like this in my
DSP.
"..\..\..\idl\common.idl".
Is there any way to force the IDE to not do this and make it use only
"\idl\common.idl".

Hope i am clear..... Please let me know if you would like more information
for understanding the problem.

Thanks in advance for your time,
V


6. Relative to absolute path

7. relative path setting for include directory path

8. how to get files and folders path as per system location absolute path



Return to C#

 

Who is online

Users browsing this forum: No registered users and 46 guest