Solved

FileStream Exists Error Work Around?

Posted on 2006-10-25
1
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
C# Formatting DateTime String Value 5 67
Unlocking a column in excel using C# 17 32
locate sql commands in C# visual studio Project 6 67
How does this modal gets closed? 6 41
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 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