Can you share a local file for reading? I know, that I can use an overloading with Stream parameter, but default behavior is strange.

I'm using the following code:

var client = new Sftp();
client.Connect("Server");
client.Login("UserName", "Password");
var destinationFile = @"C:\Temp\Eee.txt";
var destination = destinationFile;
//var destination = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.Read);
client.BeginGetFile("FileName.txt", destination, null, null);
while (!File.Exists(destinationFile))
{
  Thread.Sleep(100);
}
var test = new FileStream(destinationFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// "The process cannot access the file 'C:\\Temp\\Eee.txt' because it is being used by another process."

If you uncomment line "var destination = new FileStream(…" (and comment "var destination = destinationFile;"), the test stream opens successfully.

asked 21 Jul '10, 15:35

_FRED_'s gravatar image

_FRED_
907
accept rate: 0%

edited 23 Jul '10, 07:32


It turned out we were using File.Create in the GetFile method, and apparently this implies FileShare.None, which was indeed an oversight that resulted in strange behavior. Thanks for letting us know about this! We will definitely fix it for the next release. Sorry for inconvenience!

link

answered 23 Jul '10, 10:05

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

Update: This turned out not to be the answer to Fred's question (see the other answer), but we are keeping it because someone else might still find it useful later.

Short answer:

You have to specify FileShare.ReadWrite when opening the currently-downloaded file for reading. It's a feature of the OS and there is no way around it.

Long answer:

GetFile/BeginGetFile methods actually do share the local file for reading. This is how we open it:

Stream localFile = File.Open(localPath, FileMode.Create, FileAccess.Write, FileShare.Read)

However, you probably tried accessing the file using a code like this:

Stream stream = File.OpenRead(path);

or this:

Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

and it failed.

As you can see, this fails even though we did share the file for reading. But this is not Rebex issue - identical behavior can be reproduced using the following simple code:

string path = ...
Stream s1 = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
Stream s2 = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

Why does this fail? Because you have to specify FileShare.ReadWrite when opening the file for reading. If only FileShare.Read is specified, you are actually trying to deny others to write the file at the same time, which is not possible since there is already a writer.

And indeed, the following code works fine:

string path = ...
Stream s1 = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
Stream s2 = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

It might seem a bit odd that File.OpenRead can't be used in this case and many classes that rely on it fail, but there is nothing we can do about that. Apparently, this is a feature of the filesystem itself.

link

answered 22 Jul '10, 12:20

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

edited 23 Jul '10, 10:06

Please, see my code sample. I'm using "Rebex SFTP for .NET - Trial Version" 2.0.3854.0

(23 Jul '10, 07:28) _FRED_

Thanks, it looks you are right!

(23 Jul '10, 09:59) Lukas Pokorny ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×141

Asked: 21 Jul '10, 15:35

Seen: 451 times

Last updated: 23 Jul '10, 10:06