mikaeldeity
mikaeldeity

Reputation: 31

C# save file to memory and get a path to use in a method that takes file path as input

I'm working on an application plugin. The base software has an API method that requires as input the path of a file in the disk.

I am generating the files with my application but this file has to be read just once then deleted. I'm saving the file to the disk, giving the path to the method and then deleting the file.

I would like to write to memory this file and use the API method using the path to this file. It would increase greatly performance help on other issues.

Is it possible?

I'm using .NET 4.7

Thank you!

Upvotes: 2

Views: 3452

Answers (2)

Kedar
Kedar

Reputation: 147

The base software has an API method that requires as input the path of a file in the disk.

Do you control or can you change the base software method? if the input parameter type of method of base software is type string (which it sounds like but you have not shared any code) then there's no way to pass the memory stream to that method.

if you get response back from the method after processing is finished, i.e. the base method does not require file available on the disk afterward then you can create file on disk, pass file path to the method as input parameter and then delete the file after control returns

File.Create(filePath);
baseObj.BaseMethod(filePath);
File.Delete(filePath);

However, you should show some code to get more relevant suggestion.

Upvotes: 0

Ashish Sojitra
Ashish Sojitra

Reputation: 25

Are you looking like this or something else?

string fileName = "<Your File name>";
int bufferSize = 1024;
using (var fileStream = File.Create(fileName, bufferSize, FileOptions.DeleteOnClose))
{
 // now use that fileStream to write wour data and it will auto delete after file close 
}

This will store file in disk and you ca use path and it will auto delete after file close.

Upvotes: 2

Related Questions