将FileStream作为文件的实例。在方法之间传递并在文件上工作
我正在使用一个文件处理系统,该文件要在提示下向用户返回文件。 呼叫转到端点MVC控制器,该端点在返回文件流中调用功能的函数。 返回文件
然后,端点将使用ActionResult文件(FileStream,Mime.type,filename)
;我在助手类中遇到问题,在其中创建和处理文件。
我的目标是:
1。创建文件
2.撰写其内容
3.在文件(使用.EXE文件的过程中使用过程)上第三方程序,
并在大多数操作上记录结果。
我要做的是创建一个创建文件的文件流,然后在方法之间传递文件流以完成不同的工作,例如编写读取和运行第三方程序。我还使用静态文件类(system.io.file)来记录
以前我刚刚创建了该文件并传递其路径并创建了新的流对象或使用静态类以读/写入它,但是感觉有点像丑陋的。我觉得使用对象作为资源更符合面向对象的编程。
我的思考在这里是错误的,像这样的工作只是容易出错吗?
代码;
private DataEntity db { get; }
public string FolderPath { get; set; } //Directory for files created and log file
public string LogPath { get; set; } //Logfile path
public FileStream CreateFile(int Id, bool signFile)
{
FileType fileObject = db.FileTypes.FirstOrDefault(li => li.ID == Id);
string fileName = Path.Combine(FolderPath, fileObject.Name.Replace(" ", string.Empty) + ".lic");
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.DeleteOnClose))
{
WriteFile(fileObject , file); //Method to create the file in directory
SignOrFormatFile(fileObject , file, signFile); //Method to process the file with 3rd party .exe
try
{
file.Flush();
return file;
}
catch (Exception)
{
return null;
}
}
}
public void WriteFile(FileObject file, FileStream fileStream)
{
fileStream.Flush();
using (StreamWriter fileWriter = new StreamWriter(fileStream)) //Crashes here File is in use by other process
{
string header = fileObject.Headers; modules = fileObject.Modules;string footer = fileObject.Footer;
fileWriter.WriteLine(header + modules + footer);
fileWriter.Flush();
} //the file disapears from directory when the streamWriter exits using scope
string fileContent = System.IO.File.ReadAllText(file.Name); //Example of logging
System.IO.File.AppendAllText(LogPath, "-----------\nData:\n\n" + fileContent);
}
我在代码中的问题是评论所描述的。
流动器无法打开文件,因为它认为文件已由另一个进程使用。
流动器退出范围时将其删除。
I am working with a file handling system that is to return a file to the user on prompt.
The call goes to a endpoint MVC controller that calls a function in a helper class that returns a FileStream. The endpoint then returns the file with the ActionResult
File(FileStream, Mime.Type, FileName);
I am having issues in the helper class where I create and process the file.
My goals are:
1.Create the file
2.Write its contents
3.Run a third party program on the file (with a Process using a .exe file)
And to Log the results on most actions.
What I am trying to do is create a FileStream that creates the file and then pass the FileStream between methods to do different work like writing reading and running the 3rd party program. I am also using static file classes (System.IO.File) to logg to a logfile
Previously i just created the file and passed its path around and created new stream objects or used static classes to read/write to it but it felt kind of ugly. I feel using a object as a resource is more in line with object oriented programming.
Is my thinking here just wrong and working like this is just prone to errors?
Code;
private DataEntity db { get; }
public string FolderPath { get; set; } //Directory for files created and log file
public string LogPath { get; set; } //Logfile path
public FileStream CreateFile(int Id, bool signFile)
{
FileType fileObject = db.FileTypes.FirstOrDefault(li => li.ID == Id);
string fileName = Path.Combine(FolderPath, fileObject.Name.Replace(" ", string.Empty) + ".lic");
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.DeleteOnClose))
{
WriteFile(fileObject , file); //Method to create the file in directory
SignOrFormatFile(fileObject , file, signFile); //Method to process the file with 3rd party .exe
try
{
file.Flush();
return file;
}
catch (Exception)
{
return null;
}
}
}
public void WriteFile(FileObject file, FileStream fileStream)
{
fileStream.Flush();
using (StreamWriter fileWriter = new StreamWriter(fileStream)) //Crashes here File is in use by other process
{
string header = fileObject.Headers; modules = fileObject.Modules;string footer = fileObject.Footer;
fileWriter.WriteLine(header + modules + footer);
fileWriter.Flush();
} //the file disapears from directory when the streamWriter exits using scope
string fileContent = System.IO.File.ReadAllText(file.Name); //Example of logging
System.IO.File.AppendAllText(LogPath, "-----------\nData:\n\n" + fileContent);
}
My problems in code are what the comments describe.
The streamWriter cant open the file as it thinks the file is used by another process.
The StreamWriter Removes the file when it exits scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上无需在此处冲洗文件,因为尚未书写。冲洗是我能想到为什么创建流曲人会失败的唯一原因。
,并在处置时将其放置并将其处置。由于您要求关闭文件删除文件,因此这是预期的行为。如果您想选择退出此行为,则应使用构造函数 /代码>参数
为什么要阅读刚刚写的内容?只需将信息写入日志,同时将其写入文件。您应该能够依靠框架课程做他们应该做的事情。
写入文件后,您可能需要冲洗。我不确定流媒体是否在处置时会冲洗。我不确定这是成功的,这可能取决于第三方如何处理共享文件。可能值得一试,但是您可能需要重写方法以关闭文件,然后调用第三方程序。
由于FileStream在“使用”块中,因此将处置该文件。因此,您返回的FileStream对象将无法使用。目前尚不清楚您打算如何处理结果。我本来建议返回
fileInfo
对象,但是由于您在关闭文件时删除该文件,因此也有效。There is really no need to flush the file here, since nothing is written yet. That flush is the only reason I can think of why creating the streamWriter would fail.
StreamWriter will take ownership of the stream, and dispose it when it is disposed. Since you asked for the file to be removed on close, this is expected behavior. If you want to opt out of this behavior you should use the constructor that takes a
leaveopen
parameterWhy are you reading what you just wrote? Just write the information to the log at the same time you write it to the file. You should be able to rely on the framework classes doing what they should do.
You may need to flush the file after it has been written. I'm unsure if streamwriter will flush when it is disposed. I would not be certain that this succeeds, it may depend on how the third party handles shared files. It is probably worth a try, but you may need to rewrite your method to close the file and then call the third party program.
Since the filestream is in a 'using' block, as it should, the file will be disposed. So your returned filestream object will be unusable. It is unclear what you intend to do with the result. I would have suggested returning a
FileInfo
object instead, but since you delete the file when it is closed, that would be valid either.