common file dialog - should be an easy one

C

cmh

I would think this should be easy to do, but I'm just switching to C# after
10 years in Java and havent figured out where everything is yet.

In my C# project, I need to allow the user to select a filename from the
filesystem. This all works fine and is implemented, except with one small
problem.

The file I select will always already be opened by another app. So I don't
want the dialog to actually open then file when someone clicks ok (then I get
the file already in use error), I just want it to return the filename.

Can I do this with the common file dialog?

Any help would be appreciated...

- cmh
 
J

Jeff Johnson

I would think this should be easy to do, but I'm just switching to C# after
10 years in Java and havent figured out where everything is yet.

In my C# project, I need to allow the user to select a filename from the
filesystem. This all works fine and is implemented, except with one small
problem.

The file I select will always already be opened by another app. So I
don't
want the dialog to actually open then file when someone clicks ok (then I
get
the file already in use error), I just want it to return the filename.

Can I do this with the common file dialog?

Yes, because that's all the common dialog does. It NEVER opens anything on
its own. You'd have to right-click a file in the list and choose Send To or
some other item from the context menu to actually open the file from the
dialog, and at that point it's not really the common dialog doing the
opening, but the Explorer functionality that provides the list of files.
 
C

cmh

Im clearly missing something then... unfortunately Im on another laptop
today and dont have the code in front of me. I'll check tonight and repost
with the error - what Im doing is really simple, there must be a bone-headed
mistake I have in there.

In short, I have a 'select' button on a form that brings up the dialog, you
pick the file you want and press ok. For some reason when I press ok Im
getting a "file already in use" error. Based on your comments, I must have
something in there that is actually opening the file... I'll double-check.
Thanks for the feedback...

- cmh
 
C

cmh

Here is the code snippet that is failing:

OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title = "Select QuickBooks File";
dlgOpen.Multiselect = false;
dlgOpen.Filter = "QuickBooks Files (*.qbw)|*.qbw";

if (dlgOpen.ShowDialog() == DialogResult.OK)
{
textFilename.Text = dlgOpen.FileName;
}

I have a button on a form that fires this code, hoping that I can use the
File Open dialog to select the filename. As you can see, its trying to open
a Quickbooks file that is open by the QB application already. The code works
and presents the file open dialog, and when I select the file that is in use,
I get the following error popup:

"This file is in use.
Enter a new name or close that file that's open in another program."

Clearly the file dialog is attempting to open the file, although I am not
calling that functionality explicitly - it must be coming from within
ShowDialog when you press ok.

Any thoughts - is there some kind of flag to set that will have it not try
to open the file at all?

Thanks for the response...

-cmh

Peter Duniho said:
[...]
The file I select will always already be opened by another app. So I
don't
want the dialog to actually open then file when someone clicks ok (then
I
get the file already in use error), I just want it to return the
filename.

Can I do this with the common file dialog?

Yes, because that's all the common dialog does. It NEVER opens anything
on
its own.

Actually, that's not quite true, depending on how you define "on its own".

If the OP is calling OpenFileDialog.OpenFile(), then that does actually
try to open the other file. It will try to use read-only access, but if
the file sharing mode chosen by the other application doesn't even allow
that, then the open will fail.

Of course, since the OP says he doesn't actually want the file opened
anyway, then if he is calling OpenFile(), that would be his main mistake.
Simply looking at the FileName property is a better choice. :)

To the OP: as is nearly always the case, for best results, you really need
to post a concise-but-complete code example that reliably demonstrates
your problem. There are all sorts of ways your code _might_ be trying to
open the file, but without seeing the actual code, there's no way for
anyone to comment on what _is_ actually happening.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top