Line having the issue

Peter_1985 2,806 Reputation points
2025-08-26T02:39:05.45+00:00

Hi,

Further to the error like

Exception/StackTrace - Could not find a part of the path 'c:\xxy\newfile'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)

at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)

at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)

at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)

at ScanFolder.Program.DirSearch(String directoryPath, String par_OutFile)

How to know which line is causing the issue?

Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Adiba Khan 340 Reputation points Microsoft External Staff
    2025-08-26T08:42:32.74+00:00

    Exception/StackTrace - Could not find a part of the path 'c:\xxy\newfile' This means your code is trying to access or create a file in a folder that doesn’t exists. In this case, the folder c:\xxy\ is missing.

    Problem:

    The actual issue is in your method:

    ScanFolder.Program.DirSearch(String directoryPath, String par_OutFile)

    Where you are passing c:\xxy\newfile as par_OutFile to StreamWriter.

    Since the folder c:\xxy\ doesn’t exists, .Net cannot create newfile.

    Fix:

    Before writing the file, make sure the directory exists:

    String outFile = @”c:\xxy\newfile”;

    //Ensure directory exists

    String directory = Path.GetDirectoryName(outFile);

    If(!Directory.Exists(directory))

    {

    Directory.CreateDirectory(directory);

    }

    //Now safe to create StreamWriter

    Using(StreamWriter writer = new SreamWriter(outFile, true))

    {

    writer.WriteLine(“Test content”);

    }

     

     


  2. Bruce (SqlWork.com) 79,526 Reputation points Volunteer Moderator
    2025-08-26T15:52:45.9533333+00:00

    For the stack trace to include the line number, you need to config debug info in the build and deploy the .pdb file.

    0 comments No comments

  3. Starry Night 110 Reputation points
    2025-08-27T08:57:59.53+00:00

    The Could not find a part of the path 'c:\xxy\newfile' is a runtime exception in C# that occurs when the application tries to access a directory that does not exist. This typically happens when:

    • The directory path is incorrect or misspelled.
    • The directory has been deleted or moved.
    • The application does not have the necessary permissions to access the directory.

    You can troubleshoot your code by following the following steps:

    1.Ensure that the directory path is correct and properly formatted

    string path = @"C:\NonexistentDirectory\file.txt"; 
    var files = Directory.GetFiles(path); // Error: Directory not found
    

    Fix:

    string path = @"C:\ExistingDirectory\file.txt"; // Ensure the directory exists
    if (Directory.Exists(path))
    {
    var files = Directory.GetFiles(path);
    }
    else {
    Console.WriteLine("Directory does not exist");
    }
    

    2.Create the directory if it's missing. We can use Directory.CreateDirectory to create the directory if it does not exist.

    For example:

    string path = @"C:\NewDirectory"; 
    if (!Directory.Exists(path)) 
    { 
      Directory.CreateDirectory(path); // Create the directory 
    } 
    var files = Directory.GetFiles(path);
    

    3.Check for Relative Paths

    Please make sure that relative paths are resolved correctly. We can use Path.GetFullPath to convert relative paths to absolute paths.

    For example:

    string relativePath = @"..\NonexistentDirectory\file.txt"; 
    string absolutePath = Path.GetFullPath(relativePath); // Resolve relative path 
    if (Directory.Exists(Path.GetDirectoryName(absolutePath))) 
    { 
         var files = Directory.GetFiles(absolutePath); 
    } else
     { 
        Console.WriteLine("Directory does not exist"); 
    }
    

    Based on the logs you provided(path 'c:\xxy\newfile'), your error should be due to the first reason, which is the lack of a specific file path. Is this newfile a directory? If it is a directory, what specific files are under this directory? If it is a file, what is the file extension? You can recheck it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.