views:

8132

answers:

9

Hi,
i have a Full path as given below...

C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd

No i just want the DTDs directory to be fetched from this whole part?
Can i use String methods in this?
If yes then how to fetch this directory?

Thanks for your help.... Regards

+15  A: 

Use System.IO.Path.GetDirectoryName() for the entire path, or new DirectoryInfo(path).Parent.Name for just the name of that one folder.


There is no directory named "DTDs" in the path you posted. IT looks like there's a file named "DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd", but the periods (.) in that path are not valid directory separator characters. Did you mean "DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"?

If that's the case, given that entire new path, you want something like this to return a list of files in the DTDs folder:

string path = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd";
string[] files = new DirectoryInfo(path).Parent.GetFiles();


in properties window i choose Build Type as Embedded resource.

And now we finally get to it. When you choose "Embedded Resource", the item is bundled into your executable program file. There is no direct path anymore. Instead, set your Build Type to "Content" and set "Copy to Output Directory" to "Copy Always" or "Copy if Newer".

Joel Coehoorn
Doesn't work. In fact, it returns the unwanted part only...
Mehrdad Afshari
It gives NULL value
crazy_itgal
GetDirectoryName("C:\Mehrdad\Test") returns "C:\Mehrdad"
Mehrdad Afshari
Joel,you are getting me wrong.In solution explorer in VS2008 i created a folder named DTDs under dannyGoXuk prj and kept all my files in it.Then in properties window i choose Build Type as Embedded resource.Now wen i try to get the Full path of those files i get the whole path as i posted above.but i want the path till directory DTDs.i hope i m able to clear..
crazy_itgal
+2  A: 

Calling

System.IO.Path.GetFileName

with the full directory path returns the last part of the path which is a directory name. GetDirectoryName returns the whole path of parent directory which is unwanted.

If you have a file name and you just want the name of the parent directory:

var directoryFullPath = Path.GetDirectoryName(@"C:\DTDs\mydtd.dtd");  // C:\DTDs
var directoryName = Path.GetFileName(directoryFullPath);  // DTDs
Mehrdad Afshari
GetFileName() would return "mydtd.dtd"
Joel Coehoorn
GetFileName given the directory path will return the last part. You should call it with the *directory path*, which you can get from GetDirectoryName. I explained it in the edit.
Mehrdad Afshari
I misunderstood that you were doing a two step process-thought you were calling GetFileName() on the original string.
Joel Coehoorn
Thanks for the edit. Too bad SO doesn't have IntelliSense :)
Mehrdad Afshari
No it does not help, i am geeting the files from resources and these files are in DTDs folder under DannyGoXuk Project.so i am fetching these files one by one using foreach loop and want to get the directory DTDs.But all of the above and below answers does not help.
crazy_itgal
@crazy_itgal: Given the path you mentioned in the question, what exactly you need as output?
Mehrdad Afshari
i want the path where DTD file is there.for example string path = C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDsi want this "path"...
crazy_itgal
A: 
System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )

That will return just the name of the folder containing the file.

For

C:\windows\system32\user32.dll

this will return

system32

I'm inferring that that's what you want.

harpo
Thanks Harpo u are somewhat close to my answer but here u have directory separator to get "System32" as directory but in my case there is no such "/" thing. Whereas i have files from resources and these files are in DTDs folder under DannyGoXuk Project.so i am fetching these files one by one using foreach loop and want to get the directory DTDs.Which is just given by DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd.where xhtml-math-svg-flat.dtd is a dtd file in the DTDs directory under DannyGoXuk project.
crazy_itgal
A: 

You can use:

System.IO.Path.GetDirectoryName(path);
Beatles1692
A: 

You can use Path ...

Path.GetDirectoryName(myStr);
JP Alioto
A: 

don't use string manipulation directly:

System.IO.Path.GetDirectoryName(myPath);
It dosent work out..
crazy_itgal
A: 

Use the FileInfo object...

FileInfo info = new FileInfo(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
string directoryName = info.Directory.FullName;

The file doesn't even have to really exist.

David McEwing
i am getting this error..Cannot implicitly convert type 'System.IO.FileInfo' to 'System.Reflection.FieldInfo' Because i am using the files in resources.
crazy_itgal
A: 

Path.GetDirectory on the path you have specified returns:

"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"

Try it yourself:

var path = Path.GetDirectoryName(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");

Your question is a little strange though- there is no directory called DTDs.

RichardOD
Directory DTDs is in project DannyGoXuk.i have embedded all the resource files in the DTDs directory.In the code i want to fetch this directory how to do it,this only i want to know? Thanks..
crazy_itgal
A: 

How bout this string manipulation trick...

string strFullPath = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd";
string strDirName; 
int intLocation, intLength;

intLength = strFullPath.Length;
intLocation = strFullPath.IndexOf("DTDs");

strDirName = strFullPath.Substring(0, intLocation); 

textBox2.Text = strDirName;
JimDel
Thanks JimDel but this part gave me the o/p " DTDs.xhtml-math-svg-flat.dtd ".I am just looking for DTDs.I dont want the file xhtml-math-svg-flat.dtd with it.
crazy_itgal
the use this Substring instead...strDirName = strFullPath.Substring(0, intLocation);
JimDel
Thanks Jim this only i was looking for.....
crazy_itgal
If that was the answer you needed, could you take away the down vote. Pretty please :)
JimDel
sure i would do that but i didnt voted this answer as down..
crazy_itgal
oh sorry it gal :)
JimDel