我正在使用.NET C#app实现虚拟文件从应用程序拖放到Windows资源管理器。我从here获得了实现想法和示例代码。文件删除后,应用程序会下载文件并将其写入IStream包装器。这在Windows 7 x64上运行良好,但对于大约30 MB或更大的文件来说效果非常慢,并且在Windows 8.1上占用大量CPU资源和内存。 任何想法将不胜感激。提前致谢。代码是:
1)IStreamWrapper类
private class IStreamWrapper : Stream
{
/// <summary>
/// IStream instance being wrapped.
/// </summary>
private IStream _iStream;
/// <summary>
/// Initializes a new instance of the IStreamWrapper class.
/// </summary>
/// <param name="iStream">IStream instance to wrap.</param>
public IStreamWrapper(IStream iStream)
{
_iStream = iStream;
}
/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// </summary>
public override bool CanRead
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// </summary>
public override bool CanSeek
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// </summary>
public override bool CanWrite
{
get { return true; }
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the origin parameter.</param>
/// <param name="origin">A value of type SeekOrigin indicating the reference point used to obtain the new position.</param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (offset == 0)
{
// Optimize common case to avoid creating extra buffers
_iStream.Write(buffer, count, IntPtr.Zero);
}
else
{
// Easy way to provide the relevant byte[]
_iStream.Write(buffer.Skip(offset).ToArray(), count, IntPtr.Zero);
}
}
}
2)创建IStream并写入它:
public void SetData(short dataFormat, int index, Action<Stream> streamData)
{
...
var iStream = NativeMethods.CreateStreamOnHGlobal(IntPtr.Zero, true);
if (streamData != null)
{
// Wrap in a .NET-friendly Stream and call provided code to fill it
using(var stream = new IStreamWrapper(iStream))
{
streamData(stream); // calls downloadFile(stream);
}
}
Marshal.ReleaseComObject(iStream);
...
}
// that will be called by streamData(stream)
private void downloadFile(Stream wStream) {
...
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
throw new WebException(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
}
using (Stream stream = response.GetResponseStream())
{
byte[] buffer = new byte[4096];
while ((bytesRead = stream.Read(buffer, 0, 4096)) != 0)
{
wStream.Write(buffer, 0, bytesRead);
}
}
...
}