I have to come back 5 folders in path then I need to enter 3 folders further and check if file exist.
Lets imagine two paths:
1) C:\a\b\c\d\e\f\g\
2) C:\a\2\3\4\5\test.xml
Then my program right now is on the first path.
I need to check if file test.xml (on the second path) exists.
For that I know method File.Exists(path), however I have problems with path.
I am able to come back until folder a and check if the file is there.
For example to check if the file on the path exists:
3) C:\a\test2.xml
I may use:
File.Exists(#".\.\.\.\.\.\" + #"test2.xml");
But nevertheless my attempts to navigate to second path ( 2) ) and check if this file exists I can not to that. May anyone help me with that ? Thanks in advance. Regards.
. refers to the current directory
.. refers to the directory one level above the current
In your example:
..\..\..\..\..\..\2\3\4\5\test.xml
This moves up to the a directory then traverses down to 5 where your file resides.
Something that might be helpful to test your path and ensure you are where you think you are is this:
string currentPath = Path.GetFullPath(relativePath);
And then check the value of currentPath, if it winds up somewhere you didn't expect you can debug your path traversal rather than your code.
You just need to use .. to signify a parent directory.
Try:
File.Exists(#"..\..\..\..\..\..\" + #"test2.xml");
I need to access the full path of a Resource file in my C# vs2010 project to use to create myType variable below. If I manually type the path as
#"C:\dir1\dir2\dir3\dir4\Source\dir5\dir6\Resources\myIcon.png"
the code works and accesses the icon. But if I use:
var result = Path.GetFullPath("myIcon.png")
The value in the variable result is the name of the correct directory except with two \ backslashes instead of one throughout i.e.
"C:\\dir1\\dir2\\dir3\\dir4\\Source\\dir5\\dir6\\myIcon.png"
. The problem is that I need to derive the result (file name variable) dynamically and I cant hard code it.
I don't know if I am supposed to manually get rid of the two \ and replace it with a single \ ? Or am I missing something.
var result = Path.GetFullPath("myIcon.png");
ToolboxItemWrapper myType = new ToolboxItemWrapper(
type,
#"C:\dir1\dir2\dir3\dir4\Source\dir5\ImAdmin\dir6\myIcon.png",
type.Name);
I am re-Editing my question. I have more than few problems, I am realizing after reading all the comments and the answers. So, I will mark the only answer as the answer but the one comment about being careful of using GetFullPath was also a valuable feedback. Thank you to all.
If you are seeing \\ in debugger, then it is simply debugger escaping the \ character so string appears the same way as if it were to appear as literal in the code. Clicking on the small magnifying glass will bring up dialog that shows true, human-readable value.
I have got a strange template whose extension is ".docx.amp" . Now i want to get fileName. i.e "template". Path.GetFileNameWithoutExtension() fails as it returns template.docx. Is there any other built in mechanism or i will have to go the ugle string comparison/split way. Please suggest and give some workaround
Nope, you will have to use some kind of string split, eg:
var nameWithoutExtension = filename.Split('.')[0];
I want to get title of shortcut, not file name, not description, but title.
how to get it?
I have learn to resolve its target path from here, How to resolve a .lnk in c#
but i don't find any method to get its title.
(source: ggpht.com)
(source: ggpht.com)
It sounds like you might be trying to get the title of the file the link points to, as JRL suggests.
If you're not trying to do that, I'd recommend opening up one of these .lnk files in a hex editor like XVI32. You can probably tell from there whether the Chinese name displayed is embedded in the .lnk file or is somewhere else.
If it's somewhere else, it may be an Extended File Property. There's some source code that may help with retrieving that info: Extended File Properties
If by some chance it is inside the .lnk file, I recommend looking at the Windows Shortcut Specification to get offset information and such on the location of that data.
There is a Desktop.ini hidden file in shortcuts directory, the Desktop.ini file records display strings info of shortcuts.
Desktop.ini file sample:
[LocalizedFileNames]
Windows Update.lnk=#%SystemRoot%\system32\wucltux.dll,-1
Default Programs.lnk=#%SystemRoot%\system32\sud.dll,-1
You can use the property system APIs in latest relase of Code pack:
(all the 670+ properties in the system are accesible using simple property accessors)
http://code.msdn.microsoft.com/WindowsAPICodePack
I know your current need is only limited title of lnk files. Using the above library, the sample code might look like:
ShellLink myLink = ShellObject.FromParsingName("c:\somepath\myLink.lnk");
string title = myLink.Properties.System.Title.Value;
// This is what its pointing to...
string target = myLink.Properties.System.TargetParsingPath.Value;
Please define "title". The only attributes that sound relevent are the shortcut's file name, the target's file name, and the .lnk file's description data.
Assuming you mean the title of the file the link points to, not the link itself, and that you are talking about Windows, then it's done via a feature in NTFS, alternative streams. You can access those streams using code in this article.
Looking around on creating shortcuts, looks like there's a lot of jumping through hoops with scripting objects. But am I missing something? If you have a path to the shortcut, the name should be exactly what you find in the path, not some attribute you have to look up.
Dim f As FileInfo = New FileInfo("C:\Name of shortcut.lnk")
Dim title As String = f.Name.Replace(".lnk", String.Empty)
<MyXmlType>
<MyXmlElement>Value</MyXmlElement>
</MyXmlType>
I need a small help here. I was trying to change the innertext value of here. After i change the value and save it with Xmldocument object. When I opened the file, I found that it has got saved in this format.
<MyXmlType>
<MyXmlElement>Value</MyXmlElement>
</MyXmlType>>
Please note this </MyXmlType>> ">>". I dont know what is happening. Please help...
My guess is that you were changing the inner text to something which was one character shorter, and then overwriting the file in place instead of overwriting it with a brand new file. That would mean the extra > was left from the previous version of the file.
If you could show the code you're using to write the file, that would help a lot.
Do you get the same problem if you write to a new file?