Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo

system.io.fileinfo powershell
c get fileinfo from directory
c# directoryinfo
c fileinfo name
c# fileinfo name without extension
string to fileinfo c
fileinfo.fullname c#
c# fileinfo attributes

In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information.

I have seen a lot of code that uses strings to pass directory information then they "split" and "mid" and "instr" in long incomprehensible statements until they get the part of the directory they are looking for.

Is there any good reason to pass paths as strings?

In general, I think keeping the information in FileInfo/DirectoryInfo is better. There is a lot of useful functionality in these classes, as well as a lot of safety involved in that it's much easier to check for existence of a file, see the originally specified file, etc.

The only place where I would (potentially) pass a path as as a string instead of using FileInfo and DirectoryInfo would if the path was going to be passed across AppDomains or between processes, etc.

FileInfo and DirectoryInfo both work fine across AppDomain boundaries (since they're Serializable), but they have a fair amount more overhead in this situation. If things are going back and forth a lot, it could have an impact.

I would stick with FileInfo and DirectoryInfo in this case, though, unless I found that there was a noticeable problem during my profiling, and I was trying to reduce the amount of serialized data. If I didn't run into performance issues, I'd stick with using these classes, as they provide a lot of safety and functionality.

Automating SOLIDWORKS 2019 Using Macros, Dim MyFile As FileInfo = Nothing Dim AllParts() As FileInfo AllParts = MyDir. IO namespace, is a great way to get information about a Windows folder (directory). In this case, since DirectoryInfo is a folder, passing the folder path as a string  .net - Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo . In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information. I have seen a lot of code…

Once the path is in an application (i.e. not in a plain text configuration file), no, there isn't a good reason.

The only time (I can think of) it could be useful is when interoperating with code that only accepts paths as strings.

Professional C# 7 and .NET Core 2.0, Copy(fileName1", fileName2); The first code snippet using FileInfo takes or DirectoryInfo class by passing to the constructor a string containing the path to the an exception is not thrown at construction; instead it's thrown the first time you  .net - Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo . In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information. I have seen a lot of code…

DirectoryInfo and FileInfo are awful heavy for passing around if all you need is a path. I'd be more concerned about the "split and mid and instr" junk. Learn the ways of:

Path.GetFileName Path.GetDirectoryName Path.Combine etc...

Those are from the System.IO.Path class, btw.

Professional C# 4.0 and .NET 4, DirectoryInfo and FileInfo implement roughly the same public methods as You need to instantiate these classes before each instance is associated with a FileInfo or DirectoryInfo class by passing to the constructor a string containing the path an exception will not be thrown at construction, but will instead be thrown the  .net - Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo . In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information. I have seen a lot of code…

I think you do need a class to encapsulate a file or directory path, instead of using raw strings and manipulating them using the static System.IO.Path class. However, I don't find DirectoryInfo and FileInfo suitable, because they seem to be intended more for doing file/directory manipulations instead of path operations. If you make a custom class for path manipulation, you can provide more user friendly path manipulation functionality.

Automating SolidWorks 2011 Using Macros, End Sub DirectoryInfo DirectoryInfo is a great way to get information about a The New statement allows you to pass a folder name as a string to define the FileInfo.FullName is used to return the full path and file name, including extension. .net - Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo . In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information. I have seen a lot of code…

One significant difference between path string and FileInfo to bear in mind, which can be summarized in the tests below:

A FileInfo reflects the info about the file as of when it was instantiated - it can be deleted/modified and the FileInfo won't reflect that.

[TestMethod]
        public void TestFileInfo()
        {
            var path = @"C:\Users\bjarmuz\Desktop\aybabtu.txt";
            File.WriteAllText(path, "All your base are belong to us!");
            var file = new FileInfo(path);

            Assert.IsTrue(file.Exists);
            File.Delete(file.FullName);
            Assert.IsTrue(file.Exists);
            Assert.IsFalse(File.Exists(file.FullName));
        }

  [TestMethod]
        public void TestFileInfo()
        {
            var path = @"C:\Users\bjarmuz\Desktop\aybabtu.txt";
            File.WriteAllText(path, "All your base are belong to us!");
            Thread.Sleep(1000);

            var file = new FileInfo(path);
            var date = DateTime.UtcNow;

            Assert.IsTrue(file.LastWriteTimeUtc< date);

            File.WriteAllText(path, "No!");
            Assert.IsTrue(File.GetLastWriteTimeUtc(file.FullName)> date);

            Assert.IsFalse(file.LastWriteTimeUtc > date);
        }

This can be somewhat misleading, and if your code passes around FileInfos instead of strings, you might see incorrect results for the properties such as Exists or Last[..]Time - which would not happen if you'd use the File.Get[...]() methods.

However, on the other hand, you should not rely on the File.Exists() method either - because the file can be created/deleted just after you run the tests. Proper way for that is to NOT do this check, and rather accept that it can throw the IO exception (and be ready to handle it properly). More info in this great article https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/

Also, a significant 'upside' of FileInfo/DirectoryInfo is that it protects your method (and the method consumers) from this:

void SaveEntity(Entity theThing, string path)
{
    //now, based on the signature, you don't know whether you need file path or directory path

}

//void SaveEntity(Entity theThing, DirectoryInfo path) {}
//void SaveEntity(Entity theThing, FileInfo path) {}

File System Types in .Net - .net - iOS, Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo. In my new code I am not using strings to pass directory paths or file names. Instead​  String is not PATH. So string is not the best way to represent path. FileInfo is also not a PATH, semantically it represents FILE. So this will be better if MS will provide Path object :) or you can make it yourself, especially if this is your internal code. In this way you will not need to check your PATH arguments every time you will work with them.

How to make the StorageFile.DisplayName getter exclude the , Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo. In my new code I am not using strings to pass directory paths or file names. Instead​  DirectoryInfo, FileInfo and very long path. I try to work with DirectoryInfo, FileInfo with very long path. Can i use ~ in a path or something else. I read this post but i would like to use another way that call a API.

FileInfo Class (System.IO), IO; class Test { public static void Main() { string path = Path. to use FileInfo instance methods instead of the corresponding static methods of the File class, or overwritten, and whether requests to create an existing file cause an exception​. .net - Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo . In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information. I have seen a lot of code…

FileInfo.Directory Property (System.IO), A DirectoryInfo object representing the parent directory of this file. Remarks. To get the parent directory as a string, use the DirectoryName property. Use “[IO.Path]::DirectorySeparatorChar” When You Can’t Use “Join-Path” If for some reason you can’t use Join-Path to create a path or our strategy above, instead of hard-coding the directory separator character, use the [IO.Path]::DirectorySeparatorChar property to get the correct separator for the current operating system. For example,

Comments
  • Note that you will need to create a new FileInfo/DirectoryInfo if the file has changed its existance or modified timestamps.
  • Remember, when you're passing an instance into a function, it's passing a copy of the reference, not a copy of the entire object! So all you're really passing is copy of a pointer...
  • I believe the issue isn't how many bytes are passed around, but rather the overhead of using FileInfo/DirectoryInfo, which has to probe the file in a variety of ways to cache information about it.