?
Solved

FileStream Exists Error Work Around?

Posted on 2006-10-25
1
Medium Priority
?
318 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 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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