我通过写入文件流来创建 .jpg 文件:
using (FileStream fs = new FileStream(FileName, FileMode.CreateNew, FileAccess.Write))
{
fs.Write(image, 0, image.Length);
}
我遇到的问题是,在某些情况下,文件是使用错误的访问权限创建的,更确切地说,它似乎没有任何所有者。“无法显示当前所有者。” 在高级安全设置下。唯一应该不同的是内容本身。我究竟做错了什么?使用 FileStream 时访问权限设置在哪里?
这是整个代码
public void SaveImage(string path)
{
try
{
//Get the files data
List<byte[]> files = GetFiles();
if (files.Count == 0)
throw new Exception("No data");
//Calculate the path name
string FileDirectory = Path.GetDirectoryName(path);
if (FileDirectory.Length > 0)
FileDirectory += "\\";
string FileNameBase = Path.GetFileNameWithoutExtension(path);
string FileExtension = Path.GetExtension(path);
string FinalFileName = "";
if (files.Count == 1)
{
FinalFileName = FileDirectory + FileNameBase + FileExtension;
while (File.Exists(FinalFileName))
{
FileNameBase = FileNameBase + DateTime.Now.ToString("yyyyMMddHHmmss");
FinalFileName = FileDirectory + FileNameBase + FileExtension;
}
}
else
{
FinalFileName = FileDirectory + FileNameBase + "(1)" + FileExtension;
while (File.Exists(FinalFileName))
{
FileNameBase = FileNameBase + DateTime.Now.ToString("yyyyMMddHHmmss");
FinalFileName = FileDirectory + FileNameBase + "(1)" + FileExtension;
}
}
for (int i = 0; i < files.Count; i++)
{
byte[] image = files[i];
if (files.Count > 1)
FinalFileName = FileDirectory + FileNameBase + "(" + (i + 1).ToString() + ")" + FileExtension;
else
FinalFileName = FileDirectory + FileNameBase + FileExtension;
if (image.Length == 0)
throw new Exception("No data");
//Store the image in the specified path
using (FileStream fs = new FileStream(FinalFileName, FileMode.CreateNew, FileAccess.Write))
{
fs.Write(image, 0, image.Length);
}
}
}
catch (FaultException<string> ex)
{
throw new SdkException(ex.Message, ex.Code, ex.Detail);
}
catch (FaultException ex)
{
throw new SdkException(ex.Message, ex.Code, "");
}
}