Is there anyway to set the folderBrowserDialog to start in a set folder on a network drive? I know it takes Environment.SpecialFolders but I'm not seeing what I need in there...
Set SelectedPath to a path on the network.
I don't think it's possible to a folder picker rooted to a network path.
If you mean, the FolderBrowserDialog should start with a specific folder already selected, then set the SelectedPath property before calling ShowDialog.
If you mean rooted to a specific folder, the FolderBrowserDialog can only be rooted to one of the SpecialFolders values.
Alternately, use a control like FolderView which allows rooting to a random folder. Further, its a control, so you can place it in your own form.
DISCLAIMER: I work for LogicNP Software, the developers of FolderView.
Related
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.
In my WPF app the user needs to select a folder, which path is in the company network. I use the System.Windows.Forms.FolderBrowserDialog and the following code gets executed on a button click event:
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = "\\\\company.net\\data\\_Confidential";
DialogResult result = fbd.ShowDialog();
When the FolderBrowserDialog opens, the system automatically scans for other network devices and that causes the following problem:
The network tree gets filled with other devices and causes my SelectedPath to scroll away. This is pretty annoying when a user starts searching for a special subfolder, because he has to scroll down or his selection clicks can hit a newly added device (lost focus).
How can i avoid this problem?
Thoughts:
Can I extend/overwrite the System.Environment.SpecialFolder Enum and
set fbd.RootFolder = System.Environment.SpecialFolder.MySepcialNetworkPath;
Should I access the network folder with another dialog/control?
Should I remove the "Browse..." Button in my View and instead scan the whole \\\\company.net\\data\\_Confidential path and provide a combobox/other selection control(e.g. own subfolder-tree)?
The FolderBrowserDialog is 'adopting' your PC settings depending on how your Network Discovery is configured on your PC/network. By doing so your folder browsing experience will be consistent over other applications.
Although what you see is default behavior of the FolderBrowserDialog, you may also look at this: https://stackoverflow.com/a/15440926/5793786 Solved an issue somewhat similar to yours #Frank
While I was searching for the same problem I came across this thread:
How to use OpenFileDialog to select a folder?
Where the user uses a "CommonOpenFileDialog" available in the Nuget Package "WindowsAPICodePack-Shell".
This solved my issue, though it uses the OpenFileDialog interface.
Then the network drive can just be browsed.
I am currently working on a C# WPF project. I have a FolderBrowserDialog in the System.Windows.Forms namespace. I am creating an instance of the dialog with a variable named dlg and assigning the selected path to My Documents by using the following line of code:
dlg.SelectedPath = Environment.SpecialFolder.MyDocuments.ToString();
However, this doesn't seem to make much difference. I then tried doing the same thing but with the root path but this just seems to make it that it will set the root as My Documents and you can't get out of My Documents, i.e. to C:\ or Desktop.
How can I set a default path but still allowing access to all available areas of the drive, e.g. default path to be My Documents but allow the user to go outside of My Documents to C:\ or desktop.
Thanks for any help you can provide.
You are assigning the wrong value to SelectedPath. By setting Environment.SpecialFolder.MyDocuments.ToString(), you are setting the string "MyDocuments" (or "Personal" as it has the same value in the Environment.SpecialFolder enum) to the SelectedPath. This cannot be found as it is not a valid path, so nothing gets selected.
You need to lookup the path of the special folder using Environment.GetFolderPath():
dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
This will set the path of the special folder, which the folder browse dialog will select when it opens.
say my application is installed in C:\programfiles\xyz\ I've some setting & other data files (*.dat files) in this directory too.
My application uses OpenFileDialog to open an image. Problem is that when ever user browses to some directory (say MyPictures) for an image. The current working directory of the application becomes that directory (MyPictures in this case).
After user inputs the image. I do some processing over it and save some values to imagedata.dat which will be located in the path where original application is installed.(C:\programfiles\xyz here )
In my code I'm just using FileStream("imagedata.dat",...,...) without any path prefix before the filename. Problem here is that application is crashing because it is searching for imagedata.dat in 'MyPictures\imagedata.dat'.
How can I avoid this?
You should be using absolute path names when saving data to files. The current working directory is controlled by the user, not by you (for example, if they launch your process from a shortcut then the working directory could've been changed before your process even starts up).
Also, you should never save anything under C:\Program Files during normal use. Doing this means your program needs to be running as an administrator, and unless you're doing administrator-y things then you should be able to run it as a regular user.
The correct thing to do in your case is to use the Environment.GetFolderPath() function to get the location of the ApplicationData folder and save your data under there. Just choose a sub-directory based on your application's name.
You could save it to GetCurrentDirectory then restore with SetCurrentDirectory. However, I agree wih codeka that using the appropriate GetFolderPath (probably ApplicationData, CommonApplicationData or LocalApplicationData) is a better solution.
I have a WinForms app that has a TreeView. The user can drag files from WindowsExplorer to the TreeView, and then they can drag the files back into WindowsExplorer which in affect copies the files to wherever the files were dropped. What I'm trying to do is, if the files already exist in the directory where the files are being dropped, I want to rename the files/folders being copied in ahead of time, so that there's no collision.
Here's how I'm copying files into WindowsExplorer. On the treeView's ItemDrag, I loop through the nodes of the selected node, and then package that into an array. Then, I use this code:
var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray());
dataObject.SetData(DataFormats.StringFormat, dataObject);
DoDragDrop(dataObject, DragDropEffects.Copy);
This works well, but once it ships off to Windows Explorer, it's out of my hands. How can I find out when and where the files are being copied TO and intercept that to make changes? Is this possible?
Explorer Drag & Drop is an excellent article doing what you are trying to achieve.
EDIT2: It seems that there's a C++ article available for the same on CodeProject. But I was unable to find a way of how to do it using C#.
AFAIK, there is no way to know drop target (in your case destination folder). You can look into CFSTR_FILENAMEMAP shell clipboard format, but still in this case you can only provide name mappings before (or in process) of drag-n-drop.
Also note, that default DataObject in .net has limited shell support. So if you need to use mentioned above format, you need to write your own IDataObject implementation (or take someone's implementation, good example with lot shell drag-n-drop related stuff can be found here)
Instead of putting the file names into the dataobject, create a temporary file with a unique/easily distinguishable name and place that file name into the data object's drop list instead (That file could be empty or contain some information you might need). Use a FileSystemWatcher (watching an entire drive) to detect the drop (set the filter to the temporary file name, set IncludeSubDirectories to true, and set Path to root directory of drive to watch.) Initiate the DoDragDrop. Once the unique/easily distinguishable file is dropped, the FileSystemWatcher can tell you where it was dropped and you can do whatever you need to do (e.g. delete the dropped temporary file and replace with the ones you originally wanted to drop. It is a far-from-perfect solution but might help. Better still, it might give someone an idea to come up with a better one!)
One downside is that you don't really know in which drive someone might drop the file and you may have to set up a watcher for several drives. And if you miss a drive (or a network path) then problems .....! Remember to dispose of the watchers after the drop.
There has to be a better way though. e.g. consider when you drag a file from a zip folder. The file is only extracted after the drop.
i don't think that is possible.