[英]create file with FileStream and apply FileAttributes
Is it possible while creating the file with FileStream also apply FileAttributes at the same time? 使用FileStream创建文件时是否还可以同时应用FileAttributes? I would like to create file for stream writing with FileAttributes.Temporary file attribute.
我想使用FileAttributes.Temporary文件属性创建用于流写入的文件。
You can use FileOptions.DeleteOnClose
as one of parameters. 您可以使用
FileOptions.DeleteOnClose
作为参数之一。 File will be automatically removed after you finish your operations and dispose a stream. 完成操作并处理流后,文件将被自动删除。
You can do this if you use the Win32 CreateFile method 如果使用Win32 CreateFile方法,则可以执行此操作
uint readAccess = 0x00000001;
uint writeAccess = 0x00000002;
uint readShare = 0x00000001;
uint createAlways = 2;
uint tempAttribute = 0x100;
uint deleteOnClose = 0x04000000;
new FileStream(new SafeFileHandle(NativeMethods.CreateFile("filename",
readAccess | writeAccess,
readShare,
IntPtr.Zero,
createAlways,
tempAttribute | deleteOnClose,
IntPtr.Zero),
true),
FileAccess.ReadWrite, 4096, true);
private static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr CreateFile(string name, uint accessMode, uint shareMode, IntPtr security, uint createMode, uint flags, IntPtr template);
}
For more information, see the MSDN documentation of CreateFile 有关更多信息,请参见CreateFile的MSDN文档。
是的,您当然也可以通过使用File.SetAttributes
方法来应用FileAttributes
Why do you need to do it all at once? 为什么您需要一次全部完成?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.