Solved

FileStream Exists Error Work Around?

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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