FileStream questions

G

Guest

I have two questions related to FileStreams.

1. Is there any way to determine whether a file has the permissions that are
required by a FileStream constructor? For example, given the following
sample:

Dim fs1 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)
'...
'This is allowed.
Dim fs2 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.Read, IO.FileShare.ReadWrite)
'...
'This raises a System.IO.IOException exception.
Dim fs3 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)

Is there any way to test C:\Test.txt for the required permission before fs3
is instantiated (and thus raises an exception)? Note that in general, my app
would not have knowledge of fs1 or fs2 when fs3 is instantiated.

2. Once a FileStream has been created, is there any way to rename the
corresponding file without closing the FileStream? For example, I would like
to do this:

Dim fs As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)
'...
System.IO.File.Move("C:\Test.txt", "C:\Test2.txt")

In this case my app would have knowledge of fs if that helps.

Thanks for any help.
Lance
 
P

Peter Huang [MSFT]

Hi

Comments in line.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Thread-Topic: FileStream questions
thread-index: AcTb06DoX9JlGYIfSxObK+HsyPZnQw==
X-WBNR-Posting-Host: 68.190.169.55
From: "=?Utf-8?B?bGpsZXZlbmQ=?=" <[email protected]>
Subject: FileStream questions
Date: Mon, 6 Dec 2004 12:39:05 -0800
Lines: 34
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.vb:247190
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I have two questions related to FileStreams.

1. Is there any way to determine whether a file has the permissions that are
required by a FileStream constructor? For example, given the following
sample:

Dim fs1 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)
'...
'This is allowed.
Dim fs2 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.Read, IO.FileShare.ReadWrite)
'...
'This raises a System.IO.IOException exception.
Dim fs3 As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)

Is there any way to test C:\Test.txt for the required permission before fs3
is instantiated (and thus raises an exception)? Note that in general, my app
would not have knowledge of fs1 or fs2 when fs3 is instantiated.

So far we use the try...catch method to detect if we can open the file with
required permission. So if the open operataion will raise exception, we can
catch it and know that the current job will fail.
If the three open operation is under your control, I think you may try use
a third variable to indicate current file's status. So that the other open
user will check to see if we can open the file.

2. Once a FileStream has been created, is there any way to rename the
corresponding file without closing the FileStream? For example, I would like
to do this:

Dim fs As New System.IO.FileStream("C:\Test.txt", IO.FileMode.OpenOrCreate,
IO.FileAccess.ReadWrite, IO.FileShare.Read)
'...
System.IO.File.Move("C:\Test.txt", "C:\Test2.txt")

In this case my app would have knowledge of fs if that helps.

We can not do that, because once process A is opened the file, the file
will be in open status, the other process(the file move operation) can not
move it, because we need to delete the Test.txt. Can you tell me what is
your scenario? Maybe there is another way.
 
G

Guest

Thanks a lot for the info. I can get along fine with the current
functionality, but I wanted to make sure that there wasn't a better technique.

I do have one more question though. Is there any way to determine if a file
is being used by another application and, if so, the name of that
application? For example, if you open a file in Excel and then attempt to
change the name of the file in Windows Explorer, you get an error message
that includes the name of the application (i.e., Microsoft Office Excel) that
has the file open.

Thanks again,
Lance
 
P

Peter Huang [MSFT]

Hi

I think there is no other easy way to do that.
If you wants to know which process open which file, you may try to use the
handle tool in the sysinternals website.
e.g. if you have open the testworkbook.xls with excel.exe, you can use the
command line as below.
handle testworkbook
And you will get the output as below.
EXCEL.EXE pid: 608 E:\Test\TestWorkBook.xls

Then you can parse the output to get the result.

Hope this helps.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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