473,383 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

FileStream questions

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
Nov 21 '05 #1
9 2111
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=?=" <lj******@nospam.nospam>
Subject: FileStream questions
Date: Mon, 6 Dec 2004 12:39:05 -0800
Lines: 34
Message-ID: <49**********************************@microsoft.co m>
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!TK2MSFT NGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gblXref: 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 arerequired 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 fs3is instantiated (and thus raises an exception)? Note that in general, my appwould 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 liketo 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.

Thanks for any help.
Lance


Nov 21 '05 #2
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=?=" <lj******@nospam.nospam>
Subject: FileStream questions
Date: Mon, 6 Dec 2004 12:39:05 -0800
Lines: 34
Message-ID: <49**********************************@microsoft.co m>
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!TK2MSFT NGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gblXref: 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 arerequired 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 fs3is instantiated (and thus raises an exception)? Note that in general, my appwould 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 liketo 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.

Thanks for any help.
Lance


Nov 21 '05 #3
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

Nov 21 '05 #4
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

Nov 21 '05 #5
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.

Nov 21 '05 #6
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.

Nov 21 '05 #7
Hi

In addition, you can get the handle.exe in the link below.
http://www.sysinternals.com/ntw2k/freeware/handle.shtml
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.

Nov 21 '05 #8
Hi

In addition, you can get the handle.exe in the link below.
http://www.sysinternals.com/ntw2k/freeware/handle.shtml
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.

Nov 21 '05 #9
Thanks!

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
9
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was...
5
by: Chris Fink | last post by:
How do I load a string into a FileStream without going to disk? For example, string abc = "This is a string"; How do I load abc into a FileStream? FileStream input = new FileStream(.....);
0
by: majiofpersia | last post by:
Hi Everyone and thanks in advance, I am trying to create a webservices which will read a binary file and create another file based on that first one and sends back the new file to the client. ...
0
by: lh | last post by:
The following method only works when i give the ASP.net account full permissions on the directory. It doesn't work when i give the directory Modify, Read &Execute, List Folder Contents, Read, and...
0
by: ljlevend | last post by:
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...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
6
by: rn5a | last post by:
What's the difference between the 'Stream' object & the 'FileStream' object? A file can be opened using the following code snippets: -------------------- 'create a File object & StreamReader...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.