How does one keep DirectoryInfo's Constructor from always prefixing the app's path in it.

Joe 96 Reputation points
2021-01-27T16:56:14.85+00:00

I hope this is the right forum.
I am attempting to write a .net Core application, and I cannot seem to get ahold of a particular directory. Every time I try to create a DirectoryInfo object, it appends whatever I pass in as the parameter to the present working directory as the FullName.

I have tried passing in:
@"c:\Users\myUsername\Desktop\test"
@"c:\Users\myUsername\Desktop\test\"
"c:\Users\myUsername\Desktop\test"
and
"c:\Users\myUsername\Desktop\test\"

I don't know what I'm doing wrong, but in the following example I get:

System.IO.IOException
HResult=0x8007007B
Message=The filename, directory name, or volume label syntax is incorrect. : 'C:\Users\myUsername\Source\repos\ChangeExtensions\ChangeExtensions\bin\Debug\netcoreapp3.1\‪c:\Users\myUsername\Desktop\test'
Source=System.IO.FileSystem
StackTrace:
at System.IO.Enumeration.FileSystemEnumerator1.CreateDirectoryHandle(String path, Boolean ignoreNotFound) at System.IO.Enumeration.FileSystemEnumerator1.Init()
at System.IO.Enumeration.FileSystemEnumerator1..ctor(String directory, Boolean isNormalized, EnumerationOptions options) at System.IO.Enumeration.FileSystemEnumerable1..ctor(String directory, FindTransform transform, EnumerationOptions options, Boolean isNormalized)
at System.IO.Enumeration.FileSystemEnumerableFactory.FileInfos(String directory, String expression, EnumerationOptions options, Boolean isNormalized)
at System.IO.DirectoryInfo.InternalEnumerateInfos(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
at System.IO.DirectoryInfo.GetFiles(String searchPattern, EnumerationOptions enumerationOptions)
at System.IO.DirectoryInfo.GetFiles(String searchPattern)
at ChangeExtensions.Program.ChangeExtensions(DirectoryInfo folder, String oldExtension, String newExtension) in C:\Users\myUsername\Source\repos\ChangeExtensions\ChangeExtensions\Program.cs:line 25
at ChangeExtensions.Program.Main(String[] args) in C:\Users\myUsername\Source\repos\ChangeExtensions\ChangeExtensions\Program.cs:line 15

using System;
using System.IO;

namespace ChangeExtensions
{
    class Program
    {
        static void Main(string[] args)
        {

            string dirPath = "‪c:\\Users\\myUsername\\Desktop\\test";

            DirectoryInfo d = new DirectoryInfo(dirPath);
            //The d variable is populated with: "C:\Users\myUsername\Source\repos\ChangeExtensions\ChangeExtensions\bin\Debug\netcoreapp3.1\‪c:\Users\myUsername\Desktop"
            ChangeExtensions(d, ".text", ".txt");
        }
public static void ChangeExtensions(DirectoryInfo folder, string oldExtension, string newExtension)
    {
        //Get the files that have the extention
        FileInfo[] files = folder.GetFiles("*" + oldExtension);

        //iterate through and rename them
        for(int i = 0; i < files.Length; i++)
        {
            //double-Check
            if(files[i].Extension.ToLower().Equals(oldExtension.ToLower()))
            {
                string newFullName = Path.ChangeExtension(files[i].FullName, newExtension);

                //Rename
                files[i].MoveTo(newFullName);

            }
        }            
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,470 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,135 questions
{count} votes

2 additional answers

Sort by: Most helpful
  1. Castorix31 82,316 Reputation points
    2021-01-27T17:42:08.957+00:00

    Re-type your string

    There is a ? at beginning if I copy-paste it :

    "?c:\Users\myUsername\Desktop\test";


  2. Karen Payne MVP 35,286 Reputation points
    2021-01-27T23:21:35.637+00:00

    Hello,

    Even though a batch file worked for you here is how it can be done.

    Full source.

    Class

    Note the check for if the file already exists. In this case it's deleted, you could simply bypass it.

    using System.IO;
    using System.Linq;
    
    namespace ChangeFileExtensions
    {
        public static class FileHelpers
        {
            public static void RenameExtensions(string path, string originalExtensions, string replacementExtension)
            {
                new DirectoryInfo(path).GetFiles($"*.{originalExtensions}")
                    .ToList()
                    .ForEach(myFile =>
                    {
                        var filename = Path.ChangeExtension(myFile.Name, $".{replacementExtension}");
                        var tempName = Path.Combine(path, filename);
    
                        if (File.Exists(tempName))
                        {
                            File.Delete(tempName);
                        }
    
                        File.Move(myFile.Name, filename);
    
                    });
            }
        }
    }
    

    Console

    Program.cs

    using System;
    using static ChangeFileExtensions.FileHelpers;
    
    namespace ChangeFileExtensions
    {
        class Program
        {
            static void Main(string[] args)
            {
                RenameExtensions(
                    AppDomain.CurrentDomain.BaseDirectory,
                    "text",
                    "txt");
            }
        }
    }
    
    0 comments No comments