Solved

FileStream Exists Error Work Around?

Posted on 2006-10-25
1
312 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now