Problem with writing to file

L

Lauchlan M

Hi

I retreive a BLOB from my database and want to write it to file, like:

<<
// load imgECGImage from the BLOB

string SQLText = "SELECT ECG_Image FROM SessionData WHERE SessionID = "
+ Request.QueryString["SessionID"] + " and HospitalID = " +
Request.QueryString["SessionDataID"];
NexusDB.ADOProvider.NxCommand objCommand = new
NexusDB.ADOProvider.NxCommand(SQLText, nxConnection);

objCommand.FetchBlobs = true;
byte[] bteTemp = (byte[]) objCommand.ExecuteScalar();

string StoreItAt =
@"D:\MyWebTemp\SessionID"+Request.QueryString["SessionID"]+"HospID"+Request.
QueryString["HospitalID"]+ @"\ECGBlob.jpg";

FileStream fs = new FileStream(StoreItAt, FileMode.OpenOrCreate,
FileAccess.Write); // ** <-- error occurs here **
fs.Write(bteTemp, 0,bteTemp.Length);
fs.Close();
What I want it to do is create or overwrite any existing file. Both the
general directory D:\MyWebTemp\ and the particular directory
D:\MyWebTemp\SessionID140HospIDDV2\ may not exist before this operation, and
I want the whole path to be autocreated.

However, I get the error:

<<
Could not find a part of the path
"D:\MyWebTemp\SessionID140HospIDDV2\ECGBlob.jpg".
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a
part of the path "E:\MyWebTemp\SessionID140HospIDDV2\ECGBlob.jpg".

Source Error:


Line 197: string StoreItAt =
@"E:\MyWebTemp\SessionID"+Request.QueryString["SessionID"]+"HospID"+Request.
QueryString["HospitalID"]+ @"\ECGBlob.jpg";
Line 198:
Line 199: FileStream fs = new FileStream(StoreItAt,
FileMode.OpenOrCreate, FileAccess.Write);
Line 200: fs.Write(bteTemp, 0,bteTemp.Length);
Line 201: fs.Close();
Thanks!

Lauchlan M
 
H

Hugo Wetterberg

Do it like this instead.

FileInfo file=new FileInfo(storeAt);
if(!file.Directory.Exists)
file.Directory.Create();

FileStream fs=file.Create();

/Hugo

Hi

I retreive a BLOB from my database and want to write it to file, like:

<<
// load imgECGImage from the BLOB

string SQLText = "SELECT ECG_Image FROM SessionData WHERE SessionID = "
+ Request.QueryString["SessionID"] + " and HospitalID = " +
Request.QueryString["SessionDataID"];
NexusDB.ADOProvider.NxCommand objCommand = new
NexusDB.ADOProvider.NxCommand(SQLText, nxConnection);

objCommand.FetchBlobs = true;
byte[] bteTemp = (byte[]) objCommand.ExecuteScalar();

string StoreItAt =
@"D:\MyWebTemp\SessionID"+Request.QueryString["SessionID"]+"HospID"+Request.
QueryString["HospitalID"]+ @"\ECGBlob.jpg";

FileStream fs = new FileStream(StoreItAt, FileMode.OpenOrCreate,
FileAccess.Write); // ** <-- error occurs here **
fs.Write(bteTemp, 0,bteTemp.Length);
fs.Close();
What I want it to do is create or overwrite any existing file. Both the
general directory D:\MyWebTemp\ and the particular directory
D:\MyWebTemp\SessionID140HospIDDV2\ may not exist before this operation, and
I want the whole path to be autocreated.

However, I get the error:

<<
Could not find a part of the path
"D:\MyWebTemp\SessionID140HospIDDV2\ECGBlob.jpg".
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a
part of the path "E:\MyWebTemp\SessionID140HospIDDV2\ECGBlob.jpg".

Source Error:


Line 197: string StoreItAt =
@"E:\MyWebTemp\SessionID"+Request.QueryString["SessionID"]+"HospID"+Request.
QueryString["HospitalID"]+ @"\ECGBlob.jpg";
Line 198:
Line 199: FileStream fs = new FileStream(StoreItAt,
FileMode.OpenOrCreate, FileAccess.Write);
Line 200: fs.Write(bteTemp, 0,bteTemp.Length);
Line 201: fs.Close();
Thanks!

Lauchlan M
 
L

Lauchlan M

Do it like this instead.
FileInfo file=new FileInfo(storeAt);
if(!file.Directory.Exists)
file.Directory.Create();

FileStream fs=file.Create();

Thanks Hugo, that solved this problem.

Lauchlan M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top