简体   繁体   English

文件流重命名文件

[英]Filestream renaming file

I'm trying to read a file as filestream, renaming the file, and send the file in the form of filestream to a Document Management System(DMS).我正在尝试将文件作为文件流读取,重命名文件,然后以文件流的形式将文件发送到文档管理系统 (DMS)。

I've seen so many questions in StackOverflow asking about renaming the file in c#, and most of the reply suggested using File.Move .我在 StackOverflow 中看到了很多关于在 c# 中重命名文件的问题,并且大部分回复都建议使用 File.Move 。

However, I was wondering If don't want to actually rename the file, and I would like rename to file once the file has become filestream.但是,我想知道是否不想实际重命名文件,并且一旦文件成为文件流,我想重命名为文件。 I want to explore the alternative solution other than File.Move.我想探索 File.Move 以外的替代解决方案。

I've tried to rename the file by the following approach, however, It seems that the name property of filestream object is read-only.我尝试通过以下方法重命名文件,但是,filestream 对象的 name 属性似乎是只读的。

//This is my attempt
FileStream fs = new FileStream()
fs.Name = "new_name" //<-- not working

Additional Info:附加信息:

Yes, I'm now trying to amend some legacy code without proper documentation, where the code apparently indicated that the library of the DMS has a parameter to pass a filestream object into it and upload.是的,我现在正在尝试修改一些没有适当文档的遗留代码,其中代码显然表明 DMS 的库有一个参数可以将文件流对象传递给它并上传。 Therefore, I assumed the DMS does read the Name property of the filestream.因此,我假设 DMS 确实读取了文件流的 Name 属性。

The reason why I want to directly change the name of the filestream is that I want to change at least as possible since the one who wrote this code has already gone and I also don't have the document of the DMS.之所以要直接改文件流的名字,是因为写这段代码的人已经走了,我也没有DMS的文档,我想至少改一下。 Also, the document will be stored after finished uploading, this is the reason why I don't want to change to name of the document.另外,文件上传完成后会被保存,这就是我不想更改文件名称的原因。

//This is how the actual program code looked like
FileStream input = File.OpenRead(uploadFile_path);
obj.Update();
obj.Fetch();

But after listening to all your suggestion, I think the safest way to solve this problem will be:但是听了你的所有建议后,我认为解决这个问题最安全的方法是:

  1. Create a copy of the file创建文件的副本
  2. Rename the copy of the file重命名文件副本
  3. Upload the filestream of the file上传文件的文件流
  4. Remove the file删除文件

If your document management system (DMS apparently) is using the FileStream.Name Property (which seems weird to say the least) you are out of luck, this can't be changed (easily).如果您的文档管理系统(显然是 DMS)正在使用FileStream.Name Property (至少可以说这看起来很奇怪),那么您就不走运了,这无法(轻松)更改。

  • You will have to see if there is an override to take the file name in your DMS call您将必须查看是否存在覆盖以在 DMS 调用中获取文件名
  • Or rename it before you open it或者在打开之前重命名

Eg例如

System.IO.File.Move("oldfilename", "newfilename");

Or because this is stackoverflow, you could set the name with reflection或者因为这是 stackoverflow,您可以使用反射设置名称

Note : i do not recommend this, this may change with future versions of .net, and who knows what issues you could have, however it does work to change the Name property注意:我不建议这样做,这可能会随着 .net 的未来版本而改变,谁知道您可能会遇到什么问题,但是更改 Name 属性确实有效

// some FileStream 
FileStream file = new FileStream(@"D:\test.txt", FileMode.Open);

var myField = file.GetType()
                  .GetField( "_fileName", BindingFlags.Instance | BindingFlags.NonPublic)

// set the name
myField.SetValue(file, "blah");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM