Solved

FileStream Exists Error Work Around?

Posted on 2006-10-25
1
315 Views
Last Modified: 2012-05-05
The "CreateNew" throws an error when identical files exist in the destination directory as it should.

Problem: I need some method to direct traffic (so to speak) like: IF (!FILE.EXISTS)

 
  System.IO.FileStream fs = null;

  byte[] byteArray = null;

  foreach (string file in arraylist1)
  {
      byteArray = (f);

      fs = new FileStream("C:\\" + file, FileMode.CreateNew, FileAccess.ReadWrite);  

          if (byteArray != null)
          {
               IF (!FILE.EXISTS)   // Write file otherwise skip to next array file
               {
                    fs.Write(byteArray, 0, byteArray.Length);
               }              

0
Comment
Question by:kvnsdr
1 Comment
 
LVL 11

Accepted Solution

by:
Expert1701 earned 250 total points
ID: 17807354
You can check if the file did not previously exist:

  if (File.Exists("C:\\" + file))
    using (FileStream fs = new FileStream("C:\\" + file, FileMode.CreateNew, FileAccess.ReadWrite))
      fs.Write(byteArray, 0, byteArray.Length);

Or check if the file could not be overwritten:

  try
  {
    using (FileStream fs = new FileStream("C:\\" + file, FileMode.CreateNew, FileAccess.ReadWrite))
      fs.Write(byteArray, 0, byteArray.Length);
  }
  catch (IOException)
  {
  }

Or a combination:

  if (File.Exists("C:\\" + file))
    try
    {
      using (FileStream fs = new FileStream("C:\\" + file, FileMode.CreateNew, FileAccess.ReadWrite))
        fs.Write(byteArray, 0, byteArray.Length);
    }
    catch (IOException)
    {
      // Report Error
    }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET 2008 VB and C# 6 39
Release Dynamically Allocated Memory in C# 3 38
"Emulate" TAB key when press Enter Key 3 48
Code works but breaks when I add one section 4 20
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question