Reputation: 33
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:
Upvotes: 3
Views: 6706
Reputation: 81493
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).
E.g
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");
Upvotes: 2