Find UNC path for a DFS folder target - c#

I'm trying to read files from a folder which is hosted within a DFS namespace. DirectoryInfo is unable to handle it, claiming the path doesn't exist, so I find myself needing to resolve to conventional UNC paths. This I can do to a certain extent: I can take the first part i.e. \\domain\data and map it to \\fileserver1\share by calling NetDfsGetInfo() from netapi32.dll as seen in questions elsewhere on SO.
The problem is, that's not enough. Within my DFS path is another folder, which isn't a real folder, it is I believe a folder with a folder target, which points to a directory not in the root of the target share. So while the folder path in DFS is \\domain\data\documents it resolves to \\fileserver1\share\data\documents.
Is there a way I can determine this programatically? If I pass \\domain\data\documents to NetDfsGetInfo() all I get back is \\fileserver1\share, and I haven't yet found anything that lets me examine that folder and figure out where that goes.
If only DirectoryInfo could just handle all this transparently for me like Windows Explorer does.

Related

File path for c# when moved to a memory stick

sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.

Go to parent directory in System.Net.FtpClient

I'm using System.Net.FtpClient to work with FTP server in my web application.
After creating a directory and set it as working directory and uploading files in it, I want to go back one step (the parent directory) to do the same for other folders and files, but I don't know how to get the parent and set it as working directory.
I assume you use a relative path to enter the subdirectory like below.
ftpClient.SetWorkingDirectory("subdir");
Were you using an absolute path, I assume you would know how to open the parent directory.
To enter a parent directory, use "..":
ftpClient.SetWorkingDirectory("..");
See Representations of paths by operating system and shell on Wikipedia.
You can always use the absolute path:
ftpClient.SetWorkingDirectory("/path/to/parent/directory");

Finding a directory or file without name?

We have a some files or directories with their absolute paths.
These files or directories will be renamed in a process. We can't get the new names from the process at all, but we have the root directory full name(full absolute path)
Now, I wanna find that items again
Is there a unique key or something for directories or files to find them without the exact name?
No, there is nothing directly available on regular NTFS from C# code to do so.
You can
compute some sort of hashes of files yourself + size check to find them again after rename (if they simply renamed)
use events from FileSystemWatcher to track file movements
add alternative streams to files if they will not be stripped by "the process" to use as custom markers.

Get full path of a file/folder located not in the project path

I am trying to get the fullpath of a file/folder that is located in location
D://Sachin//Reports//excel.xls
but when I call Path.getfullpath("excel.xls") it returns me the location
C:\users\Projname\bin\debug\etc.
What I need is the path other than what it locates in the project because this path i need to give in my data source connection string.
You would have search all files on the system until you found 'excel.xls'. This would take a very long time, and would not even guarantee that you find the correct 'excel.xls'. Do not do this.
I would recommend one of the following solutions instead:
Use a constant full path. For example, the file must be located at 'C:\excel.xls'.
Use a constant relative path. For example, the file must be in the same directory as the executable.
Store the path in some kind of configuration file, such as the registry.
Obtain the path from the user, such as with an OpenFileDialog.

full path of a folder in c#

i need to get the full folder path in a windows project using c#.I tried with path.getFulPath(filename).bt it returns the application path+filename.how can i get the actual path like "D:\eclipse_files\ads_data"?
A relative path such as myfile.txt is always resolved in relation to the current working directory.
In your case the current working directory seems to be D:\eclipse_files\ads_data so your relative file path gets resolved to D:\eclipse_files\ads_data\myfile.txt when you call Path.GetFullPath.
To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly.
You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods.
Your question is not very clear, but I think you're looking for this:
string path = Path.GetDirectoryName(filename);
If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from?
If this is what you ask it is not possible. Have you considered that there might be several files called 'doc.txt' on your harddrives?
The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow.

Categories