I have a Windows Form application and I use a custom control that allows users to select and save image. However I need to insert some restrictions. The easy one was to select only jpg files but now I need to restrict the users to be able to see and select only jpg images with certain pattern in the name and if possible (I think I've seen this implemented in other windows applications) I want this pattern to stay in the File Name field and the user to not be able to delete it.
what I have done till now :
I have the restriction set:
fileNameFilter = "All files (*.jpg)|" + ImageNameFilter + "*.jpg";
openFileDialog1.Filter = fileNameFilter;
I can show the user what pattern he needs to look for:
openFileDialog1.FileName = fileNameFilter;
However there are two things that I still can't accomplish. Here is a print screen to make it clear:
First - the pattern is shown but I can delete it when it's made like this and I want if it's possible the File name field to be Read only or in other words the user should not be able to delete what I have put there.
Second - this is the list with previously opened files even though the file don't match the name restrictions/filters the user is still able to select form the list and save that file. I need, again if possible either to clean this list when the File Dialog is opened or somehow to stop the user from being able to save this file but i think the second will require too much extra business logic so I prefer just to clean the list.
Trying to control rigidly what's shown in a file dialog through a filter or a file name pattern is not going to work. The user can always type their own pattern into the file name edit box.
What you need to do is use the file dialog's facilities for controlling what objects are displayed. Unfortunately the C# wrapper doesn't expose this functionality that is offered by the underlying Win32 controls.
If you need to support XP, then you need to listen for the CDN_INCLUDEITEM notification. This is sent for each item in the folder. You therefore get the opportunity to either allow or deny the inclusion of each item.
For the dialogs used in Vista and later it's different. These dialogs use IFileDialog. You need to call the SetFilter method to add a filter. That filter is your implementation of IShellItemFilter which again controls inclusion using the IncludeItem method.
It's going to be a bit messy to make all this happen from C#, but this is the correct way to do what you ask. Once you do this, there's no need to even think about trying to make the file name edit box read-only. Because the dialog will only offer up the items that you have allowed to be included.
No.
Two solutions: check the filename after Open-click (which you should do anyway), or create your own control that displays files that do match your filename pattern.
Related
I'm making a documentation program and I built a search engine with text boxes,combo boxes,and check list boxes. I haven't written a code that actually does the searching, there are just buttons and the components.
Anyways the files I want to access are in one folder in the C drive and in that folder there are many other folders and subfolders. I want the user to either type in the desired name of the folder,or select from a combo box.Normally we can open a folder with this code:
Process.Start(#"C:\Users\melek\Desktop\svn");
I don't want to write Users\melek etc each time in code. I need a code that grabs the users selected folder or file name(using combo boxes and text boxes) and find the folder and display it in windows form.
Is it possible to convert the selection from the text box or combo box to string and use the Process.Start command?
We solved this problem like this:
we obtained a path and used this code:
listBox1.DataSource = System.IO.Directory.GetFileSystemEntries(path1);
What you're trying to do requires several steps, so let's discuss them in turn.
First you need to get a path from the user so your program knows where to search . A FolderBrowserDialog attached to a button provides this. No need to hard code any paths since you can fetch the path string from this dialog.
Next you can use the Path, Directory and File classes to perform operations such as, fetching a list of all subfolders from a path, extracting paths of paths as strings for additional searching, and checking properties or flags on files and folders in case you decide to support more advanced search options.
I would suggest understanding these core file system classes more if you're going to be doing any amount of development that interacts with the file system. They are your bread and butter. I've linked to the MSDN documentation for each, for your convenience.
I am working on a Version Control/File Sync System for Windows. It would be great to have a checkbox at the bottom of the classical save file dialog with the option to check/uncheck for my file versioning.
Is it possible to listen for opened save file dialogs by any program (word etc.) and replace/override that dialog with a customized one (with an additional checkbox)?
If the checkbox is checked, another window should pop-up where the user could enter some additional metadata. After that the data is stored in a local database.
I already worked with the approach by dmihailescu (link provided) but it's very complex and I do not know how to modify that example to listen for opened save file dialogs by other programs.
http://www.codeproject.com/Articles/19566/Extend-OpenFileDialog-and-SaveFileDialog-the-easy?msg=4779306#xx4779306xx
Another approach is to use the FileSystemWatcher but that's very expensive to watch the whole system and it's not very comfortable because the user has to be asked for any created file if he/she wants to version control it.
I hope someone could help me to solve that problem or has some additional tips / approaches.
Thank you.
Edit: Use-case
A user has to write a documentation and creates a new word-doc. When he/she clicks the Save as menu entry of word, my customized save file dialog should pop-up with a checkbox at the bottom, if this file should be versioned or not. If the checkbox is "active" a new window should appear where the user could enter additional metadata. After that the data should be stored in local database.
In my case, only the metadata (like the path etc.) should be stored in the database. Let's suppose a user stores the same file in two different directotries (one file is "older" and one file is the current one). If the user opens an older version of this file, my system should recognize that a "newer" one is already stored in another place and synchronize those files.
That should just be a very easy example.
You have two pieces of functionality: save and version-control. Both of the tasks are actually rather complicated. Therefore you shouldn't mix them. You better off using standard Windows API to save file and do not change that. Think about how you'd support several different Windows releases and how painful that would be.
I assume you have your own UI, and do not integrate with, say, Windows Explorer (like Tortoise Svn or Dropbox). In this case you can do version-control magic first and then just save the end file using standard API.
If you do integrate with Windows Explorer, I suggest you to have a look at Tortoise svn source code.
Can I allow a user to delete a file, under Windows, that is in use by my application?
In my scenario I have a "quick add" directory that is monitored by my application. When it sees a new image show up it creates a new slide to display in an automated slide show. I would also like to allow my users to delete (and/or rename) a file from my "quick add" directory, and remove it from the slide show.
Is there a way I can flag the file that notifies Windows that I'm okay for it to remove the file while my application is using it?
Is there a way I can flag the file that notifies Windows that I'm okay for it to remove the file while my application is using it?
Yes. In Win32 this is dwShareMode passed to CreateFile(). This is a bitfield of what you would like to permit another process to do. The bit you are looking for is FILE_SHARE_DELETE which allows a delete or a rename while open. However, for the most polite behavior I would recommend including all 3, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE.[1]
Since you're asking about C# and not Win32, in the .NET world these are included in the FileShare enumeration which you can pass when opening a file, eg. in File.Open.
Note that these flags don't let you allow a rename of a file's parent directory while it is opened by name. That will fail regardless of sharing due to limitations in the kernel (technically in ntfs.sys IIRC).
Footnote
1: Editorial comment: It is a shame that passing 0 (which many people do without thinking) happens to be the least permissive option, or that more people writing code on Windows don't realize what this parameter does and pass these three flags.
If you call a Delete() or Rename() method onto the Control that is using the file, you can remove it from the PictureBox before it deletes or renames the file.
There shouldn't be much of an issue with doing multiples. With a Checkbox on the Control, you just check each Control to see if that property is true, then call the appropriate method.
I am trying to put a filter on my C# openFileDialog that excludes certain file extensions. For example I want it to show all files in a directory that are not .txt files.
Is there a way to do this?
I don't think this is possible. The way the filter is set up, is that you can choose which files to show, but I don't think there's a way to show "All files except...". Come to think of it, have you ever seen an Open File Dialog in Windows that has this? I don't think I've ever seen one.
Your best bet is to let them choose all files, and then prompt the user if they select one that isn't allowed OR filter it down to all the possible files that you can deal with.
There is no direct way to do this using the BCL OpenFileDialog.
I can think of a couple of options:
1) Make a filter that just has all of the types you do want to support. This would be my recommendation, since that's the most common way of going about this type of operation.
2) Use something along the lines of this custom OpenFileDialog implementation. You could then override the OnFileNameChanged() method to potentially disable the "Open" button if the selected file has a .txt extension.
3) Let the user pick a .txt file, throw up an error dialog, and reopen the file dialog. This feels clunky and not too great to me, though....
Found with Google search "OpenFileDialog"
EDIT: Sorry about not expanding on the EXCLUDE aspects. You may not need to go to this extreme, but it would meet your needs....
Do a recursive directory search of all the files that the user may choose from. (Hopefully that's a small set of folders.)
Uniquely identify the extensions on those files. (System.IO.Path.GetExtension() and Linq's .Distint() method may work well here)
Remove the ".txt" entry from the list.
Construct a filter string by looping through these extensions.
I'm afraid this isn't possible. You'll either have to
a) Include a long list of all the extensions that should be allowed, or
b) Allow all extensions, and then use a simple check in code that reopens the dialog if the selected file has extension .txt.
Also, could you provide a bit more context for this question? I'm having trouble envisaging a scenario where I might explicitly need to exclude a certain extension. You can't probably get away with just a filter (and maybe a bit of checking in code) in almost all situations.
Here's a completely different approach, which you're unlikely to use.
Instead of using OpenFileDialog, analyze the specific features you need and create your own dialog box. You could easily provide whatever filtering you want, but then the difficulty moves over to the implementing and maintaining the new UI.
I don't suggest that you do this, but sometimes users are rather insistent upon what they need.
Another alternate: Instead of using a dialog, come up with something completely different that fits well within the app. This may not be possible, but then we don't know much about the app.
You cannot set a filter to exclude extensions from file dialogs.
You could however implement a delegate for the FileOk event on the dialog. This event fires just before the file the user selected will be accepted, and the event arguments provide a Cancel property that you can set to disallow selection.
It is not as elegant as actually making the wrong files invisible, but it will allow you to disallow selection of the wrong kind of file.
PS: Do not forget to give the user feedback why the file was not accepted, otherwise they may wonder why the dialog is not closing when they pick a 'txt' file.
I would like to know how to filter files in a open file dialog (in winforms)
based on a regular expression. Files have all same extensions (.xml).
Big files are split up into several files with the same name only to be separated with _1 ...
We only want to show the files without _1 (first data file)
the open file dialog has a property filter but i dont know how to specify this in our filename format, hence the regular expression.
Thankx,
Niki
I don't think you can do it with the OpenFileDialog's Filter property, which just filters list of files based on extension.
I think you'll have to let the user choose an xml file, validate and then pop up the dialog again if its a _1 file. You can subscribe to the FileOK event and slot in this validation in there. You can use regular expressions to validate the filename here. That's the best that can be done.. I guess.
The OpenFileDialogEx described in this CodeProject article is an extension of the standard OpenFileDialog. The primary intention of that extension is to modify the display of the dialog, but there are some additional bells and whistles. For example, OFDEx adds a few events, for File changed, Folder change, etc.
Someone pointed out that the CDN_INCLUDEITEM notification seems like it would satisfy the desire to filter the list of files shown in the dialog,. It seems like it would, but it does not. The CDN_INCLUDEITEM does not do what you might think or want.
According to this MSDN Mag article,
If you create your dialog with
OFN_ENABLEINCLUDENOTIFY, Windows sends
your hook procedure a CDN_INCLUDEITEM
notification for every item it adds to
the open list. If you return FALSE,
Windows excludes the item. The problem
is, Windows doesn't notify you for
ordinary files, only pseudo-objects
like namespace extensions. When you
read the documentation through a
magnifying glass, the print is
perfectly clear: "The dialog box
always includes items that have both
the SFGAO_FILESYSTEM and
SFGAO_FILESYSANCESTOR attributes,
regardless of the value returned by
CDN_INCLUDEITEM." Apparently the
Redmondtonians added CDN_INCLUDEITEM
for their own purposes, which didn't
include filtering ordinary file names.
In other words, in response to CDN_INCLUDEITEM, you cannot return FALSE for regular files, to exclude them from the dialog. In contrast to the doc which says, the response from the CDN_INCLUDEITEM is ignored for regular files, in my experience, the CDN_INCLUDEITEM is not even sent for regular files, at least not on my Vista machine.
So is it possible to exclude files dynamically? Well, yes, in C++; In response to the CDN_FOLDERCHANGED message, you can get and set the contents of the CListCtrl that contains the files. I haven't figured out how to set this list in C#.
The OpenFileDialog does not support this. An alternative is to use a 3rd party control like FileView which lets you filter items using any criteria you wish such as regular expressions.
You should be able to do it with the following filter:
Data Files|*_1.xml
I'm not sure how to do it in C# with WinForms, but in C++, what you would do is install a custom hook procedure and listen for the CDN_INCLUDEITEM notification. Then, you check each filename against your regex. See http://msdn.microsoft.com/en-us/library/ms646960(VS.85).aspx#_win32_Filters.