473,758 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileStream Question

The following code which is supposed to dynamically create files with
incrementing names is throwing an exception due to the inclusion of the
DateTime.Now component of the sReportSave string:

string sReportSave = ( DateTime.Now + "Report.Dat " );
FileStream fs = new FileStream( sReportSave , FileMode.Create );

Is my approach completely wrong or is there different way to modify the
FileStream so that .NETclients used by multiple users could save files
incrementally without saving to the same file? The logic that I would
normally use for this sort of thing seems unsuitable due to the fact
that I am trying to make the clients run independantly of each other
even if used on the same machine.

-E

Dec 6 '06 #1
2 1918

You want to specify a pattern for the time, especially if you'll be
using the value in a filename (slashes are not valid in a file name).

DateTime.Now.To String("yyyy-MM-dd--HH-mm-ss-") + "Report.dat "

But you have to be concerned with multiple clients making the file at
the same time so you may want to include a counter within a loop and
error checking:
int i = 1;
bool alreadyUsed = false;
FileStream fs;
do {
string name = null;
try {
name =
String.Format(" {0:yyyy-MM-dd--HH-mm-ss}--{1:000}-Report.dat",
DateTime.Now, i++);
fs = new FileStream(name , FileMode.Create );
} catch(IOExcepti on ex) {
if (File.Exists(na me)) {
alreadyUsed = true;
} else {
throw;
}
}
} while(alreadyUs ed);
If you think the likihood of two processes creating the same file is
high then you can check File.Exists before trying to create the file
to be more efficient, but you still need the error handling since
another process could create the file between your File.Exists call
and the new FileStream call.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 6 Dec 2006 12:41:01 -0800, ew******@gmail. com wrote:
>The following code which is supposed to dynamically create files with
incrementing names is throwing an exception due to the inclusion of the
DateTime.Now component of the sReportSave string:

string sReportSave = ( DateTime.Now + "Report.Dat " );
FileStream fs = new FileStream( sReportSave , FileMode.Create );

Is my approach completely wrong or is there different way to modify the
FileStream so that .NETclients used by multiple users could save files
incrementall y without saving to the same file? The logic that I would
normally use for this sort of thing seems unsuitable due to the fact
that I am trying to make the clients run independantly of each other
even if used on the same machine.

-E
Dec 6 '06 #2
Thank you! That is precisely what I needed to know. Thank you very much
for your assistance.

-E

Samuel R. Neff wrote:
You want to specify a pattern for the time, especially if you'll be
using the value in a filename (slashes are not valid in a file name).

DateTime.Now.To String("yyyy-MM-dd--HH-mm-ss-") + "Report.dat "

But you have to be concerned with multiple clients making the file at
the same time so you may want to include a counter within a loop and
error checking:
int i = 1;
bool alreadyUsed = false;
FileStream fs;
do {
string name = null;
try {
name =
String.Format(" {0:yyyy-MM-dd--HH-mm-ss}--{1:000}-Report.dat",
DateTime.Now, i++);
fs = new FileStream(name , FileMode.Create );
} catch(IOExcepti on ex) {
if (File.Exists(na me)) {
alreadyUsed = true;
} else {
throw;
}
}
} while(alreadyUs ed);
If you think the likihood of two processes creating the same file is
high then you can check File.Exists before trying to create the file
to be more efficient, but you still need the error handling since
another process could create the file between your File.Exists call
and the new FileStream call.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
Dec 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
391
by: Kai Bohli | last post by:
Hi all ! This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the printer port. I found the SendFileToPrinter example below in a knowledge base at MS site It works, but I need to replace the filestream with a memorystream cause there's no need to write files and then load them again just to print. So I rewrote the procedure and the call. The problem is of course that it dosen't work. If I replace the...
9
5855
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was working with the StreamReader object I got a deadlock when I tried reading the file while my external application was writing to it. So far I am able to create a FileStream object and open the file in question (CrawlerBackup.txt). But I am unable to...
0
1014
by: Tim Kohler | last post by:
I'm trying to open a file for writing in a server process. I do not want any other process to be able to write to the file while I have it open, but I do want to allow other programs to read from it. Seems simple enough. I've done this with CFile in MFC. I'm getting a problem trying to use the .NET FileStream class. I can open the files with other applications (notepad) and they open up in 'readonly' mode which is what I expect. Can any of...
5
12747
by: Chris Fink | last post by:
How do I load a string into a FileStream without going to disk? For example, string abc = "This is a string"; How do I load abc into a FileStream? FileStream input = new FileStream(.....);
0
1537
by: lh | last post by:
The following method only works when i give the ASP.net account full permissions on the directory. It doesn't work when i give the directory Modify, Read &Execute, List Folder Contents, Read, and Write permissions (all at the same time) I'm only trying to READ the file. Why do i have to give it full permissions? public static string ReadFileContents(string filePath) {
7
2313
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
0
331
by: dave | last post by:
my other Question: im Parsing data from a File.... should i be using string objects or stringbuilder i was reading about both of them and im looping through files upwards of 20meg to 100 meg of information The docs say sting class creates a new class each time it returns a substring or change in the string..... and Stringbuilder use its internal buffers Can you Explain a bit more about string builder which on i should use and the best...
2
4079
by: =?Utf-8?B?TWlzY2hh?= | last post by:
It is my understanding that I can create an XmlTextReader from a variety of sources, including a stream object. I am developing a web application that reads some data from an Xml file. Everything works fine if I use the XmlTextReader to open the file (i.e. pass it the file name): Dim reader As XmlTextReader = New XmlTextReader("<file_name>") But I get an access error if I try to open the file using a FileStream object and then passing...
7
1950
by: Piotrekk | last post by:
Hi Probability of this phenomena is like 1/200. The only code where I am dealing with file is the following: using (FileStream fs = new FileStream(this.FilePath, FileMode.Open,FileAccess.Read)) { try {
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10076
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7287
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.