简体   繁体   中英

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).

I've seen so many questions in StackOverflow asking about renaming the file in c#, and most of the reply suggested using 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.

I've tried to rename the file by the following approach, however, It seems that the name property of filestream object is read-only.

//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. Therefore, I assumed the DMS does read the Name property of the filestream.

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. 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).

  • You will have to see if there is an override to take the file name in your DMS call
  • 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

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

// 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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