Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenFileDialog Appears To Lock File 2

Status
Not open for further replies.

abraxas

Programmer
Jan 15, 2001
75
AU
Hello,
I've got an app in which user browses (openfiledialog) to a file, opens it, and the code retrieves the filename and path.

Drama occurs when further on in the app we go to open the file. In this case an app (call it myapp.exe "path\filename") started using System.Diagnostics.Process.Start(). The message is "unable to open file" or something to that effect (depends on what function is trying to open it after the the openfiledialog).

This works when the parameters (file paths) for myapp.exe are hardcoded in the Arguments method but not when when constructed using the openfiledialog(). What's the go with this? I had the same problem connecting to an interbase file (using openfiledialog) and researched for a month on what was going on but only came up with a work round using paths stored in an INI file.

Here is the code I'ver whittled down to basic form

string strPathData = null;
OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShoeDialog() == Dialog.Result.OK)
{
dlg.OpenFile();
strPathData = dlg.FileName;
}

dlg.Dispose();

char c = (char)34; // Rabit ears
System.Diagnostics.Process.StartInfo psi = new System.Diagnostics.Process.StartInfo();
psi.FileName = "myApp.exe"
psi.Arguments = c + strPathData + c; // Works with "c:\\mypath\\myfile.dat" here instead
psi.WorkingDirectory = "C:\\mypath";
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);

I've checked the Argument string and it is congruent to the hardcoded string.
As I said it only fails when the OpenFileDialog is used. I just wish I could find some article on why it doesn't work. Is it some kind of lock? The file in question is a Clarion DAT file all by itself. No clarion application running (or installed!).

Thanks in advance for any pointers
Anthony
 
dlg.OpenFile()?

That is not the correct usage of that method. This is why your files are being locked. Your dialog is opening them - probably to return a readstream to you and then you dispose of the dialog without closing the readstream.

You should really use OpenFileDialog just to locate the file, not open it.

string filepath = null;

OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
filepath = dlg.FileName;
}

Then use the filename to send to your app so your app can open the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top