How to select the file which is already opened usi

2019-02-22 01:04发布

I am trying to select the file which is already opened in quickbook software.

code :

 OpenFileDialog ofdBrowseVInv = new OpenFileDialog();

            ofdBrowseVInv.Title = "Locate QuickBook Company File";
            ofdBrowseVInv.Filter = "QuickBook Company File (*.qbw,*.qbw)|*.qbw;*.qbm";
            ofdBrowseVInv.FileName = "";


           if (ofdBrowseVInv.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strfilename = ofdBrowseVInv.InitialDirectory + ofdBrowseVInv.FileName;

            }

After selecting the file .. i am getting message : File in use

can any one tell me how can i select the file which is already opened...

File in use Screen shot

3条回答
▲ chillily
3楼-- · 2019-02-22 01:23

Locating a file using an open file dialog will not produce the error you describe so I assume that you subsequently are opening the file. Opening a file that is already opened by another process will often fail because the other process has a lock on the file so it may very will be that opening a file that is "already opened" is impossible.

However, if the other process (presumably QuickBooks) has opened the file with read sharing allowed you can open the file by specifying the correct sharing mode. Normally, you will open a file for reading using the File.OpenRead method which will use FileShare.Read. This fails because the other process already has the right to write to the file. However, you can specify the sharing mode FileShare.ReadWrite by using File.Open method instead:

using (var fileStream = File.Open(strfilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
  // ... read from fileStream
}

If the other process allows read sharing you will be able to read the file. However, if it does not you will not be able to read the file and there is not much you can do about it except close the file in the other application.

查看更多
一夜七次
4楼-- · 2019-02-22 01:35

This code worked for me perfectly.

ofdBrowseVInv.ValidateNames = false;
查看更多
登录 后发表回答