How do I determine a mapped drive's actual path? - c#

How do I determine a mapped drive's actual path?
So if I have a mapped drive on a machine called "Z" how can I using .NET determine the machine and path for the mapped folder?
The code can assume it's running on the machine with the mapped drive.
I looked at Path, Directory, FileInfo objects, but can't seem to find anything.
I also looked for existing questions, but could not find what I'm looking for.

I expanded on ibram's answer and created this class (which has been updated per comment feedback). I've probably over documented it, but it should be self-explanatory.
/// <summary>
/// A static class to help with resolving a mapped drive path to a UNC network path.
/// If a local drive path or a UNC network path are passed in, they will just be returned.
/// </summary>
/// <example>
/// using System;
/// using System.IO;
/// using System.Management; // Reference System.Management.dll
///
/// // Example/Test paths, these will need to be adjusted to match your environment.
/// string[] paths = new string[] {
/// #"Z:\ShareName\Sub-Folder",
/// #"\\ACME-FILE\ShareName\Sub-Folder",
/// #"\\ACME.COM\ShareName\Sub-Folder", // DFS
/// #"C:\Temp",
/// #"\\localhost\c$\temp",
/// #"\\workstation\Temp",
/// #"Z:", // Mapped drive pointing to \\workstation\Temp
/// #"C:\",
/// #"Temp",
/// #".\Temp",
/// #"..\Temp",
/// "",
/// " ",
/// null
/// };
///
/// foreach (var curPath in paths) {
/// try {
/// Console.WriteLine(string.Format("{0} = {1}",
/// curPath,
/// MappedDriveResolver.ResolveToUNC(curPath))
/// );
/// }
/// catch (Exception ex) {
/// Console.WriteLine(string.Format("{0} = {1}",
/// curPath,
/// ex.Message)
/// );
/// }
/// }
/// </example>
public static class MappedDriveResolver
{
/// <summary>
/// Resolves the given path to a full UNC path if the path is a mapped drive.
/// Otherwise, just returns the given path.
/// </summary>
/// <param name="path">The path to resolve.</param>
/// <returns></returns>
public static string ResolveToUNC(string path) {
if (String.IsNullOrWhiteSpace(path)) {
throw new ArgumentNullException("The path argument was null or whitespace.");
}
if (!Path.IsPathRooted(path)) {
throw new ArgumentException(
string.Format("The path '{0}' was not a rooted path and ResolveToUNC does not support relative paths.",
path)
);
}
// Is the path already in the UNC format?
if (path.StartsWith(#"\\")) {
return path;
}
string rootPath = ResolveToRootUNC(path);
if (path.StartsWith(rootPath)) {
return path; // Local drive, no resolving occurred
}
else {
return path.Replace(GetDriveLetter(path), rootPath);
}
}
/// <summary>
/// Resolves the given path to a root UNC path if the path is a mapped drive.
/// Otherwise, just returns the given path.
/// </summary>
/// <param name="path">The path to resolve.</param>
/// <returns></returns>
public static string ResolveToRootUNC(string path) {
if (String.IsNullOrWhiteSpace(path)) {
throw new ArgumentNullException("The path argument was null or whitespace.");
}
if (!Path.IsPathRooted(path)) {
throw new ArgumentException(
string.Format("The path '{0}' was not a rooted path and ResolveToRootUNC does not support relative paths.",
path)
);
}
if (path.StartsWith(#"\\")) {
return Directory.GetDirectoryRoot(path);
}
// Get just the drive letter for WMI call
string driveletter = GetDriveLetter(path);
// Query WMI if the drive letter is a network drive, and if so the UNC path for it
using (ManagementObject mo = new ManagementObject()) {
mo.Path = new ManagementPath(string.Format("Win32_LogicalDisk='{0}'", driveletter));
DriveType driveType = (DriveType)((uint)mo["DriveType"]);
string networkRoot = Convert.ToString(mo["ProviderName"]);
if (driveType == DriveType.Network) {
return networkRoot;
}
else {
return driveletter + Path.DirectorySeparatorChar;
}
}
}
/// <summary>
/// Checks if the given path is a network drive.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns></returns>
public static bool isNetworkDrive(string path) {
if (String.IsNullOrWhiteSpace(path)) {
throw new ArgumentNullException("The path argument was null or whitespace.");
}
if (!Path.IsPathRooted(path)) {
throw new ArgumentException(
string.Format("The path '{0}' was not a rooted path and ResolveToRootUNC does not support relative paths.",
path)
);
}
if (path.StartsWith(#"\\")) {
return true;
}
// Get just the drive letter for WMI call
string driveletter = GetDriveLetter(path);
// Query WMI if the drive letter is a network drive
using (ManagementObject mo = new ManagementObject()) {
mo.Path = new ManagementPath(string.Format("Win32_LogicalDisk='{0}'", driveletter));
DriveType driveType = (DriveType)((uint)mo["DriveType"]);
return driveType == DriveType.Network;
}
}
/// <summary>
/// Given a path will extract just the drive letter with volume separator.
/// </summary>
/// <param name="path"></param>
/// <returns>C:</returns>
public static string GetDriveLetter(string path) {
if (String.IsNullOrWhiteSpace(path)) {
throw new ArgumentNullException("The path argument was null or whitespace.");
}
if (!Path.IsPathRooted(path)) {
throw new ArgumentException(
string.Format("The path '{0}' was not a rooted path and GetDriveLetter does not support relative paths.",
path)
);
}
if (path.StartsWith(#"\\")) {
throw new ArgumentException("A UNC path was passed to GetDriveLetter");
}
return Directory.GetDirectoryRoot(path).Replace(Path.DirectorySeparatorChar.ToString(), "");
}
}

I can't remember where I found this, but it works without p/invoke. It's what rerun posted before.
you need to reference System.Management.dll:
using System.IO;
using System.Management;
code:
public void FindUNCPaths()
{
DriveInfo[] dis = DriveInfo.GetDrives();
foreach( DriveInfo di in dis )
{
if(di.DriveType == DriveType.Network)
{
DirectoryInfo dir = di.RootDirectory;
// "x:"
MessageBox.Show( GetUNCPath( dir.FullName.Substring( 0, 2 ) ) );
}
}
}
public string GetUNCPath(string path)
{
if(path.StartsWith(#"\\"))
{
return path;
}
ManagementObject mo = new ManagementObject();
mo.Path = new ManagementPath( String.Format( "Win32_LogicalDisk='{0}'", path ) );
// DriveType 4 = Network Drive
if(Convert.ToUInt32(mo["DriveType"]) == 4 )
{
return Convert.ToString(mo["ProviderName"]);
}
else
{
return path;
}
}
Update:
Explicitly running as administrator will not show mapped drives. Here is an explanation of this behaviour:
https://stackoverflow.com/a/11268410/448100
(in short: administrator has a different user context, so no access to mapped drives of normal user)

Here are some code samples:
Using P/Invoke
All of the magic derives from a Windows function:
[DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int WNetGetConnection(
[MarshalAs(UnmanagedType.LPTStr)] string localName,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
ref int length);
Example invocation:
var sb = new StringBuilder(512);
var size = sb.Capacity;
var error = Mpr.WNetGetConnection("Z:", sb, ref size);
if (error != 0)
throw new Win32Exception(error, "WNetGetConnection failed");
var networkpath = sb.ToString();

I've written a method for this. It returns a UNC path if it is a mapped drive, otherwise it returns the path unchanged.
public static string UNCPath(string path)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Network\\" + path[0]))
{
if (key != null)
{
path = key.GetValue("RemotePath").ToString() + path.Remove(0, 2).ToString();
}
}
return path;
}
EDIT
You now can use the Method even with already UNC paths. The above version of the method throws an exception if given a UNC path.
public static string UNCPath(string path)
{
if (!path.StartsWith(#"\\"))
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Network\\" + path[0]))
{
if (key != null)
{
return key.GetValue("RemotePath").ToString() + path.Remove(0, 2).ToString();
}
}
}
return path;
}

I think you can use the "Network" key From the "Current User" Hive, In the Registry.
The Mapped Drives Are Listed There With Their Shared Path On Server.
If there is no mapped drive in the system, so there is no "Network" Key In The "Current User" Hive.
Now, I'm using this way, no external dll nor anything else.

You can use WMI to interrogate the Win32_LogicalDrive collection on your machine. Here is an example of how to do it with scripting. Changing this over to C# is pretty well explained in other places.
Slightly modified VB.NET code from the article:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strComputer = "."
Dim objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colDrives = objWMIService.ExecQuery("Select * From Win32_LogicalDisk Where DriveType = 4")
For Each objDrive In colDrives
Debug.WriteLine("Drive letter: " & objDrive.DeviceID)
Debug.WriteLine("Network path: " & objDrive.ProviderName)
Next
End Sub
End Class

I could not replicate ibram's or Vermis' answer due to the problem I mentioned in a comment under Vermis' answer, about a type initializer exception.
Instead, I discovered I could query for all the drives currently on the computer and then loop through them, like so:
using System.IO; //For DirectoryNotFound exception.
using System.Management;
/// <summary>
/// Given a local mapped drive letter, determine if it is a network drive. If so, return the server share.
/// </summary>
/// <param name="mappedDrive"></param>
/// <returns>The server path that the drive maps to ~ "////XXXXXX//ZZZZ"</returns>
private string CheckUNCPath(string mappedDrive)
{
//Query to return all the local computer's drives.
//See http://msdn.microsoft.com/en-us/library/ms186146.aspx, or search "WMI Queries"
SelectQuery selectWMIQuery = new SelectQuery("Win32_LogicalDisk");
ManagementObjectSearcher driveSearcher = new ManagementObjectSearcher(selectWMIQuery);
//Soem variables to be used inside and out of the foreach.
ManagementPath path = null;
ManagementObject networkDrive = null;
bool found = false;
string serverName = null;
//Check each disk, determine if it is a network drive, and then return the real server path.
foreach (ManagementObject disk in driveSearcher.Get())
{
path = disk.Path;
if (path.ToString().Contains(mappedDrive))
{
networkDrive = new ManagementObject(path);
if (Convert.ToUInt32(networkDrive["DriveType"]) == 4)
{
serverName = Convert.ToString(networkDrive["ProviderName"]);
found = true;
break;
}
else
{
throw new DirectoryNotFoundException("The drive " + mappedDrive + " was found, but is not a network drive. Were your network drives mapped correctly?");
}
}
}
if (!found)
{
throw new DirectoryNotFoundException("The drive " + mappedDrive + " was not found. Were your network drives mapped correctly?");
}
else
{
return serverName;
}
}
This works for x64 Windows 7, for .NET 4. It should be usable in case you're getting that exception that was mentioned above.
I did this using the stuff given from MSDN and bits from ibram's or Vermis' answers, though it was a bit difficult to find specific examples on the MSDN. Resources used:
MSDN : Win32_LogicalDisk Class
MSDN : System.Management namespace
MSDN : WMI Queries example:
using System;
using System.Management;
class Query_SelectQuery
{
public static int Main(string[] args)
{
SelectQuery selectQuery = new
SelectQuery("Win32_LogicalDisk");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(selectQuery);
foreach (ManagementObject disk in searcher.Get())
{
Console.WriteLine(disk.ToString());
}
Console.ReadLine();
return 0;
}
}

QueryDosDevice translates a drive letter into the path that it expands to.
Note that this will translate ALL drive letters, not just those that are mapped to network connections. You need to already know which are network paths, or to parse the output to see which are network.
Here's the VB signature
Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (
ByVal lpDeviceName As String,
ByVal lpTargetPath As String,
ByVal ucchMax As Integer) As Integer
And the C# one
[DllImport("kernel32.dll")]
static extern uint QueryDosDevice(string lpDeviceName, IntPtr lpTargetPath, uint ucchMax);

Similar to ibram's answer with a few modifications:
public static String GetUNCPath(String path) {
path = path.TrimEnd('\\', '/') + Path.DirectorySeparatorChar;
DirectoryInfo d = new DirectoryInfo(path);
String root = d.Root.FullName.TrimEnd('\\');
if (!root.StartsWith(#"\\")) {
ManagementObject mo = new ManagementObject();
mo.Path = new ManagementPath(String.Format("Win32_LogicalDisk='{0}'", root));
// DriveType 4 = Network Drive
if (Convert.ToUInt32(mo["DriveType"]) == 4)
root = Convert.ToString(mo["ProviderName"]);
else
root = #"\\" + System.Net.Dns.GetHostName() + "\\" + root.TrimEnd(':') + "$\\";
}
return Recombine(root, d);
}
private static String Recombine(String root, DirectoryInfo d) {
Stack s = new Stack();
while (d.Parent != null) {
s.Push(d.Name);
d = d.Parent;
}
while (s.Count > 0) {
root = Path.Combine(root, (String) s.Pop());
}
return root;
}

Seems it's need a P/Invoke: Converting a mapped drive letter to a network path using C#
This guy built a managed class to deal with it: C# Map Network Drive (API)

You can also use WMI Win32_LogicalDisk to get all the information you need. use the ProviderName from the class to get the UNC path.

As far as Windows cares, what's needed is a call to WNetGetConnection. I don't know of a front-end for that in .NET, so you may have to call it via P/Invoke (fortunately, it has only one parameter, the P/Invoke code isn't too awful).

This post describe how to get the absolute path of a drive which is mapped to a local folder?
For example, I have a "c:\test" folder and an "x:" drive which is
mapped to c:\test.
I'm looking for a function which will return "c:\test" when I pass in
"x:"
The answer is:
SUBST uses DefineDosDevice (XP and later) to create the drive/path
mapping. You can use the QueryDosDevice to get the path of a SUBSTed
drive:
[DllImport("kernel32.dll")]
private static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
static String GetPhysicalPath(String path)
{
if (String.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
// Get the drive letter
string pathRoot = Path.GetPathRoot(path);
if(String.IsNullOrEmpty(pathRoot))
{
throw new ArgumentNullException("path");
}
string lpDeviceName = pathRoot.Replace("\\", "");
const String substPrefix = #"\??\";
StringBuilder lpTargetPath = new StringBuilder(260);
if (0 != QueryDosDevice(lpDeviceName, lpTargetPath, lpTargetPath.Capacity))
{
string result;
// If drive is substed, the result will be in the format of "\??\C:\RealPath\".
if (lpTargetPath..ToString().StartsWith(substPrefix))
{
// Strip the \??\ prefix.
string root = lpTargetPath.ToString().Remove(0, substPrefix.Length);
result = Path.Combine(root, path.Replace(Path.GetPathRoot(path), ""));
}
else
{
// TODO: deal with other types of mappings.
// if not SUBSTed, just assume it's not mapped.
result = path;
}
return result;
}
else
{
// TODO: error reporting
return null;
}
}

Here is a solution that doesn't care if it is local or remote
private string uncpath_check(string path)
{
string rval = path;
string driveprefix = path.Substring(0, 2);
string unc;
if (driveprefix != "\\")
{
ManagementObject mo = new ManagementObject();
try
{
mo.Path = new ManagementPath(String.Format("Win32_LogicalDisk='{0}'", driveprefix));
unc = (string)mo["ProviderName"];
rval = path.Replace(driveprefix, unc);
}
catch
{
throw;
}
}
if (rval == null)
{ rval = path; }
return rval;
}

Related

How can I change the file name use c#? [duplicate]

How do I rename a file using C#?
Take a look at System.IO.File.Move, "move" the file to a new name.
System.IO.File.Move("oldfilename", "newfilename");
System.IO.File.Move(oldNameFullPath, newNameFullPath);
In the File.Move method, this won't overwrite the file if it is already exists. And it will throw an exception.
So we need to check whether the file exists or not.
/* Delete the file if exists, else no exception thrown. */
File.Delete(newFileName); // Delete the existing file if exists
File.Move(oldFileName,newFileName); // Rename the oldFileName into newFileName
Or surround it with a try catch to avoid an exception.
Just add:
namespace System.IO
{
public static class ExtendedMethod
{
public static void Rename(this FileInfo fileInfo, string newName)
{
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
}
}
And then...
FileInfo file = new FileInfo("c:\test.txt");
file.Rename("test2.txt");
You can use File.Move to do it.
First solution
Avoid System.IO.File.Move solutions posted here (marked answer included).
It fails over networks. However, copy/delete pattern works locally and over networks. Follow one of the move solutions, but replace it with Copy instead. Then use File.Delete to delete the original file.
You can create a Rename method to simplify it.
Ease of use
Use the VB assembly in C#.
Add reference to Microsoft.VisualBasic
Then to rename the file:
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName);
Both are strings. Note that myfile has the full path. newName does not.
For example:
a = "C:\whatever\a.txt";
b = "b.txt";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);
The C:\whatever\ folder will now contain b.txt.
You can copy it as a new file and then delete the old one using the System.IO.File class:
if (File.Exists(oldName))
{
File.Copy(oldName, newName, true);
File.Delete(oldName);
}
public void RenameFile(string filePath, string newName)
{
FileInfo fileInfo = new FileInfo(filePath);
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
NOTE: In this example code we open a directory and search for PDF files with open and closed parenthesis in the name of the file. You can check and replace any character in the name you like or just specify a whole new name using replace functions.
There are other ways to work from this code to do more elaborate renames but my main intention was to show how to use File.Move to do a batch rename. This worked against 335 PDF files in 180 directories when I ran it on my laptop. This is spur of the moment code and there are more elaborate ways to do it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BatchRenamer
{
class Program
{
static void Main(string[] args)
{
var dirnames = Directory.GetDirectories(#"C:\the full directory path of files to rename goes here");
int i = 0;
try
{
foreach (var dir in dirnames)
{
var fnames = Directory.GetFiles(dir, "*.pdf").Select(Path.GetFileName);
DirectoryInfo d = new DirectoryInfo(dir);
FileInfo[] finfo = d.GetFiles("*.pdf");
foreach (var f in fnames)
{
i++;
Console.WriteLine("The number of the file being renamed is: {0}", i);
if (!File.Exists(Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", ""))))
{
File.Move(Path.Combine(dir, f), Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", "")));
}
else
{
Console.WriteLine("The file you are attempting to rename already exists! The file path is {0}.", dir);
foreach (FileInfo fi in finfo)
{
Console.WriteLine("The file modify date is: {0} ", File.GetLastWriteTime(dir));
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
None of the answers mention writing a unit testable solution. You could use System.IO.Abstractions as it provides a testable wrapper around FileSystem operations, using which you can create a mocked file system objects and write unit tests.
using System.IO.Abstractions;
IFileInfo fileInfo = _fileSystem.FileInfo.FromFileName("filePathAndName");
fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, newName));
It was tested, and it is working code to rename a file.
Use:
using System.IO;
string oldFilePath = #"C:\OldFile.txt"; // Full path of old file
string newFilePath = #"C:\NewFile.txt"; // Full path of new file
if (File.Exists(newFilePath))
{
File.Delete(newFilePath);
}
File.Move(oldFilePath, newFilePath);
Use:
public static class FileInfoExtensions
{
/// <summary>
/// Behavior when a new filename exists.
/// </summary>
public enum FileExistBehavior
{
/// <summary>
/// None: throw IOException "The destination file already exists."
/// </summary>
None = 0,
/// <summary>
/// Replace: replace the file in the destination.
/// </summary>
Replace = 1,
/// <summary>
/// Skip: skip this file.
/// </summary>
Skip = 2,
/// <summary>
/// Rename: rename the file (like a window behavior)
/// </summary>
Rename = 3
}
/// <summary>
/// Rename the file.
/// </summary>
/// <param name="fileInfo">the target file.</param>
/// <param name="newFileName">new filename with extension.</param>
/// <param name="fileExistBehavior">behavior when new filename is exist.</param>
public static void Rename(this System.IO.FileInfo fileInfo, string newFileName, FileExistBehavior fileExistBehavior = FileExistBehavior.None)
{
string newFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(newFileName);
string newFileNameExtension = System.IO.Path.GetExtension(newFileName);
string newFilePath = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileName);
if (System.IO.File.Exists(newFilePath))
{
switch (fileExistBehavior)
{
case FileExistBehavior.None:
throw new System.IO.IOException("The destination file already exists.");
case FileExistBehavior.Replace:
System.IO.File.Delete(newFilePath);
break;
case FileExistBehavior.Rename:
int dupplicate_count = 0;
string newFileNameWithDupplicateIndex;
string newFilePathWithDupplicateIndex;
do
{
dupplicate_count++;
newFileNameWithDupplicateIndex = newFileNameWithoutExtension + " (" + dupplicate_count + ")" + newFileNameExtension;
newFilePathWithDupplicateIndex = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileNameWithDupplicateIndex);
}
while (System.IO.File.Exists(newFilePathWithDupplicateIndex));
newFilePath = newFilePathWithDupplicateIndex;
break;
case FileExistBehavior.Skip:
return;
}
}
System.IO.File.Move(fileInfo.FullName, newFilePath);
}
}
How to use this code
class Program
{
static void Main(string[] args)
{
string targetFile = System.IO.Path.Combine(#"D://test", "New Text Document.txt");
string newFileName = "Foo.txt";
// Full pattern
System.IO.FileInfo fileInfo = new System.IO.FileInfo(targetFile);
fileInfo.Rename(newFileName);
// Or short form
new System.IO.FileInfo(targetFile).Rename(newFileName);
}
}
I couldn't find an approach which suits me, so I propose my version. Of course, it needs input and error handling.
public void Rename(string filePath, string newFileName)
{
var newFilePath = Path.Combine(Path.GetDirectoryName(filePath), newFileName + Path.GetExtension(filePath));
System.IO.File.Move(filePath, newFilePath);
}
In my case, I want the name of the renamed file to be unique, so I add a date-time stamp to the name. This way, the filename of the 'old' log is always unique:
if (File.Exists(clogfile))
{
Int64 fileSizeInBytes = new FileInfo(clogfile).Length;
if (fileSizeInBytes > 5000000)
{
string path = Path.GetFullPath(clogfile);
string filename = Path.GetFileNameWithoutExtension(clogfile);
System.IO.File.Move(clogfile, Path.Combine(path, string.Format("{0}{1}.log", filename, DateTime.Now.ToString("yyyyMMdd_HHmmss"))));
}
}
Move is doing the same = copy and delete old one.
File.Move(#"C:\ScanPDF\Test.pdf", #"C:\BackupPDF\" + string.Format("backup-{0:yyyy-MM-dd_HH:mm:ss}.pdf", DateTime.Now));
// Source file to be renamed
string sourceFile = #"C:\Temp\MaheshChand.jpg";
// Create a FileInfo
System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
// Check if file is there
if (fi.Exists)
{
// Move file with a new name. Hence renamed.
fi.MoveTo(#"C:\Temp\Mahesh.jpg");
Console.WriteLine("File Renamed.");
}
public static class ImageRename
{
public static void ApplyChanges(string fileUrl,
string temporaryImageName,
string permanentImageName)
{
var currentFileName = Path.Combine(fileUrl,
temporaryImageName);
if (!File.Exists(currentFileName))
throw new FileNotFoundException();
var extention = Path.GetExtension(temporaryImageName);
var newFileName = Path.Combine(fileUrl,
$"{permanentImageName}
{extention}");
if (File.Exists(newFileName))
File.Delete(newFileName);
File.Move(currentFileName, newFileName);
}
}
I've encountered a case when I had to rename the file inside the event handler, which was triggering for any file change, including rename, and to skip forever renaming of the file I had to rename it, with:
Making its copy
Removing the original
File.Copy(fileFullPath, destFileName); // Both have the format of "D:\..\..\myFile.ext"
Thread.Sleep(100); // Wait for the OS to unfocus the file
File.Delete(fileFullPath);
private static void Rename_File(string FileFullPath, string NewName) // nes name without directory actualy you can simply rename with fileinfo.MoveTo(Fullpathwithnameandextension);
{
FileInfo fileInfo = new FileInfo(FileFullPath);
string DirectoryRoot = Directory.GetParent(FileFullPath).FullName;
string filecreator = FileFullPath.Substring(DirectoryRoot.Length,FileFullPath.Length-DirectoryRoot.Length);
filecreator = DirectoryRoot + NewName;
try
{
fileInfo.MoveTo(filecreator);
}
catch(Exception ex)
{
Console.WriteLine(filecreator);
Console.WriteLine(ex.Message);
Console.ReadKey();
}
enter code here
// string FileDirectory = Directory.GetDirectoryRoot()
}
When C# doesn't have some feature, I use C++ or C:
public partial class Program
{
[DllImport("msvcrt", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int rename(
[MarshalAs(UnmanagedType.LPStr)]
string oldpath,
[MarshalAs(UnmanagedType.LPStr)]
string newpath);
static void FileRename()
{
while (true)
{
Console.Clear();
Console.Write("Enter a folder name: ");
string dir = Console.ReadLine().Trim('\\') + "\\";
if (string.IsNullOrWhiteSpace(dir))
break;
if (!Directory.Exists(dir))
{
Console.WriteLine("{0} does not exist", dir);
continue;
}
string[] files = Directory.GetFiles(dir, "*.mp3");
for (int i = 0; i < files.Length; i++)
{
string oldName = Path.GetFileName(files[i]);
int pos = oldName.IndexOfAny(new char[] { '0', '1', '2' });
if (pos == 0)
continue;
string newName = oldName.Substring(pos);
int res = rename(files[i], dir + newName);
}
}
Console.WriteLine("\n\t\tPress any key to go to main menu\n");
Console.ReadKey(true);
}
}

How to find list of files in "StreamingAssets" folder in Android

I have some files in my StreamingAssets folder and I am able to load them on Android as mentioned in the Unity documentation. https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html
But here I need to know the filename in advance.
Now I want to get list of all the files I have added in this directory. Following code does not work on Android. It gives directory not found exception.
DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath);
FileInfo[] info = dir.GetFiles("*.*");
So I want to know how do I get list of files in my StreamingAssets folder on Android. I need the file name so I can access the filename.
Use Better Streaming Assets, a free and open source plug in that parses APK/OBB archive to get the list of streaming assets. It also lets you read them without WWW.
Asset Store: https://www.assetstore.unity3d.com/#!/content/103788
Github: https://github.com/gwiazdorrr/BetterStreamingAssets
Hope this helps!
A simple way to solve this is to write the list of files in a .txt file before building the application and load the .txt file to access the file paths later in-game.
Add the BuildProcessor.cs script anywhere in your project, it will be executed each time you build the application.
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
class BuildProcessor : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report)
{
Debug.LogWarning("OnPreprocessBuild");
SaveStreamingAssetPaths();
}
private void SaveStreamingAssetPaths(string directory = "", string file_name = "StreamingAssetPaths")
{
List<string> paths = StreamingAssetsExtension.GetPathsRecursively(directory); // Gets list of files from StreamingAssets/directory
// You could also save paths of files in Resources
// List<string> paths = ResourcesExtension.GetPathsRecursively(directory); // Gets list of files from Resources/directory
string txtPath = Path.Combine(Application.dataPath, "Resources", file_name + ".txt"); // writes the list of file paths to /Assets/Resources/
if (File.Exists(txtPath))
{
File.Delete(txtPath);
}
using (FileStream fs = File.Create(txtPath)) {}
using(StreamWriter writer = new StreamWriter(txtPath, false))
{
foreach (string path in paths)
{
writer.WriteLine(path);
}
}
}
}
#endif
It uses the following extension methods:
For files in StreamingAssets, use StreamingAssetsExtension.cs:
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class StreamingAssetsExtension
{
/// <summary>
/// Recursively traverses each folder under <paramref name="path"/> and returns the list of file paths.
/// It will only work in Editor mode.
/// </summary>
/// <param name="path">Relative to Application.streamingAssetsPath.</param>
/// <param name="paths">List of file path strings.</param>
/// <returns>List of file path strings.</returns>
public static List<string> GetPathsRecursively(string path, ref List<string> paths)
{
var fullPath = Path.Combine(Application.streamingAssetsPath, path);
DirectoryInfo dirInfo = new DirectoryInfo(fullPath);
foreach (var file in dirInfo.GetFiles())
{
if (!file.Name.Contains(".meta"))
{
paths.Add(Path.Combine(path, file.Name)); // With file extension
}
}
foreach (var dir in dirInfo.GetDirectories())
{
GetPathsRecursively(Path.Combine(path, dir.Name), ref paths);
}
return paths;
}
public static List<string> GetPathsRecursively(string path)
{
List<string> paths = new List<string>();
return GetPathsRecursively(path, ref paths);
}
}
For files in Resources, use ResourcesExtension.cs:
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class ResourcesExtension
{
/// <summary>
/// Recursively traverses each folder under <paramref name="path"/> and returns the list of file paths.
/// It will only work in Editor mode.
/// </summary>
/// <param name="path">Relative to Application.streamingAssetsPath.</param>
/// <param name="paths">List of file path strings.</param>
/// <returns>List of file path strings.</returns>
public static List<string> GetPathsRecursively(string path, ref List<string> paths)
{
var fullPath = Path.Combine(Application.dataPath, "Resources", path);
DirectoryInfo dirInfo = new DirectoryInfo(fullPath);
foreach (var file in dirInfo.GetFiles())
{
if (!file.Name.Contains(".meta"))
{
paths.Add(Path.Combine(path, Path.GetFileNameWithoutExtension(file.Name))); // Without file extension
}
}
foreach (var dir in dirInfo.GetDirectories())
{
GetPathsRecursively(Path.Combine(path, dir.Name), ref paths);
}
return paths;
}
public static List<string> GetPathsRecursively(string path)
{
List<string> paths = new List<string>();
return GetPathsRecursively(path, ref paths);
}
/// <summary>
/// Recursively traverses each folder under <paramref name="basePath"/> and loads each file as a <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="basePath"></param>
/// <returns>Returns the loaded object of type <typeparamref name="T"/></returns>
public static List<T> LoadRecursively<T>(string basePath) where T : Object
{
List<string> paths = GetPathsRecursively(basePath);
List<T> objects = new List<T>();
foreach (var path in paths)
{
objects.Add(Resources.Load<T>(path));
}
return objects;
}
public static List<(T, string)> LoadRecursivelyWithPaths<T>(string basePath) where T : Object
{
List<string> paths = new List<string>();
GetPathsRecursively(basePath, ref paths);
List<(T, string)> list = new List<(T, string)>();
foreach (var path in paths)
{
list.Add((Resources.Load<T>(path), path));
}
return list;
}
}
You can retrieve the file paths as the following:
// ...
List<string> filePathsList = new List<string>();
TextAsset paths = Resources.Load<TextAsset>(file_name); // i.e. StreamingAssetPaths.txt
string fs = paths.text;
string[] fLines = Regex.Split(fs, "\n|\r|\r\n");
foreach (string line in fLines)
{
if (line.Length > 0)
filePathsList.Add(line);
}
// ...
// Example of loading:
string examplePath = filePathsList[0];
// If the file is in StreamingAssets:
...
path = Path.Combine(Application.streamingAssetsPath, examplePath);
byte[] data = null;
int dataLen = 0;
UnityWebRequest webRequest = UnityWebRequest.Get(path);
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
dataLen = webRequest.downloadHandler.data.Length;
data = webRequest.downloadHandler.data;
}
// do whatever you want with the data ...
// If the file is in Resources:
...
ObjectType obj = Resources.Load<ObjectType>(examplePath);
...
I think this question is already been answered....
http://answers.unity3d.com/questions/210909/android-streamingassets-file-access.html

Renaming Pinned Application [duplicate]

How do I rename a file using C#?
Take a look at System.IO.File.Move, "move" the file to a new name.
System.IO.File.Move("oldfilename", "newfilename");
System.IO.File.Move(oldNameFullPath, newNameFullPath);
In the File.Move method, this won't overwrite the file if it is already exists. And it will throw an exception.
So we need to check whether the file exists or not.
/* Delete the file if exists, else no exception thrown. */
File.Delete(newFileName); // Delete the existing file if exists
File.Move(oldFileName,newFileName); // Rename the oldFileName into newFileName
Or surround it with a try catch to avoid an exception.
Just add:
namespace System.IO
{
public static class ExtendedMethod
{
public static void Rename(this FileInfo fileInfo, string newName)
{
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
}
}
And then...
FileInfo file = new FileInfo("c:\test.txt");
file.Rename("test2.txt");
You can use File.Move to do it.
First solution
Avoid System.IO.File.Move solutions posted here (marked answer included).
It fails over networks. However, copy/delete pattern works locally and over networks. Follow one of the move solutions, but replace it with Copy instead. Then use File.Delete to delete the original file.
You can create a Rename method to simplify it.
Ease of use
Use the VB assembly in C#.
Add reference to Microsoft.VisualBasic
Then to rename the file:
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName);
Both are strings. Note that myfile has the full path. newName does not.
For example:
a = "C:\whatever\a.txt";
b = "b.txt";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);
The C:\whatever\ folder will now contain b.txt.
You can copy it as a new file and then delete the old one using the System.IO.File class:
if (File.Exists(oldName))
{
File.Copy(oldName, newName, true);
File.Delete(oldName);
}
public void RenameFile(string filePath, string newName)
{
FileInfo fileInfo = new FileInfo(filePath);
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
NOTE: In this example code we open a directory and search for PDF files with open and closed parenthesis in the name of the file. You can check and replace any character in the name you like or just specify a whole new name using replace functions.
There are other ways to work from this code to do more elaborate renames but my main intention was to show how to use File.Move to do a batch rename. This worked against 335 PDF files in 180 directories when I ran it on my laptop. This is spur of the moment code and there are more elaborate ways to do it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BatchRenamer
{
class Program
{
static void Main(string[] args)
{
var dirnames = Directory.GetDirectories(#"C:\the full directory path of files to rename goes here");
int i = 0;
try
{
foreach (var dir in dirnames)
{
var fnames = Directory.GetFiles(dir, "*.pdf").Select(Path.GetFileName);
DirectoryInfo d = new DirectoryInfo(dir);
FileInfo[] finfo = d.GetFiles("*.pdf");
foreach (var f in fnames)
{
i++;
Console.WriteLine("The number of the file being renamed is: {0}", i);
if (!File.Exists(Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", ""))))
{
File.Move(Path.Combine(dir, f), Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", "")));
}
else
{
Console.WriteLine("The file you are attempting to rename already exists! The file path is {0}.", dir);
foreach (FileInfo fi in finfo)
{
Console.WriteLine("The file modify date is: {0} ", File.GetLastWriteTime(dir));
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
None of the answers mention writing a unit testable solution. You could use System.IO.Abstractions as it provides a testable wrapper around FileSystem operations, using which you can create a mocked file system objects and write unit tests.
using System.IO.Abstractions;
IFileInfo fileInfo = _fileSystem.FileInfo.FromFileName("filePathAndName");
fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, newName));
It was tested, and it is working code to rename a file.
Use:
using System.IO;
string oldFilePath = #"C:\OldFile.txt"; // Full path of old file
string newFilePath = #"C:\NewFile.txt"; // Full path of new file
if (File.Exists(newFilePath))
{
File.Delete(newFilePath);
}
File.Move(oldFilePath, newFilePath);
Use:
public static class FileInfoExtensions
{
/// <summary>
/// Behavior when a new filename exists.
/// </summary>
public enum FileExistBehavior
{
/// <summary>
/// None: throw IOException "The destination file already exists."
/// </summary>
None = 0,
/// <summary>
/// Replace: replace the file in the destination.
/// </summary>
Replace = 1,
/// <summary>
/// Skip: skip this file.
/// </summary>
Skip = 2,
/// <summary>
/// Rename: rename the file (like a window behavior)
/// </summary>
Rename = 3
}
/// <summary>
/// Rename the file.
/// </summary>
/// <param name="fileInfo">the target file.</param>
/// <param name="newFileName">new filename with extension.</param>
/// <param name="fileExistBehavior">behavior when new filename is exist.</param>
public static void Rename(this System.IO.FileInfo fileInfo, string newFileName, FileExistBehavior fileExistBehavior = FileExistBehavior.None)
{
string newFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(newFileName);
string newFileNameExtension = System.IO.Path.GetExtension(newFileName);
string newFilePath = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileName);
if (System.IO.File.Exists(newFilePath))
{
switch (fileExistBehavior)
{
case FileExistBehavior.None:
throw new System.IO.IOException("The destination file already exists.");
case FileExistBehavior.Replace:
System.IO.File.Delete(newFilePath);
break;
case FileExistBehavior.Rename:
int dupplicate_count = 0;
string newFileNameWithDupplicateIndex;
string newFilePathWithDupplicateIndex;
do
{
dupplicate_count++;
newFileNameWithDupplicateIndex = newFileNameWithoutExtension + " (" + dupplicate_count + ")" + newFileNameExtension;
newFilePathWithDupplicateIndex = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileNameWithDupplicateIndex);
}
while (System.IO.File.Exists(newFilePathWithDupplicateIndex));
newFilePath = newFilePathWithDupplicateIndex;
break;
case FileExistBehavior.Skip:
return;
}
}
System.IO.File.Move(fileInfo.FullName, newFilePath);
}
}
How to use this code
class Program
{
static void Main(string[] args)
{
string targetFile = System.IO.Path.Combine(#"D://test", "New Text Document.txt");
string newFileName = "Foo.txt";
// Full pattern
System.IO.FileInfo fileInfo = new System.IO.FileInfo(targetFile);
fileInfo.Rename(newFileName);
// Or short form
new System.IO.FileInfo(targetFile).Rename(newFileName);
}
}
I couldn't find an approach which suits me, so I propose my version. Of course, it needs input and error handling.
public void Rename(string filePath, string newFileName)
{
var newFilePath = Path.Combine(Path.GetDirectoryName(filePath), newFileName + Path.GetExtension(filePath));
System.IO.File.Move(filePath, newFilePath);
}
In my case, I want the name of the renamed file to be unique, so I add a date-time stamp to the name. This way, the filename of the 'old' log is always unique:
if (File.Exists(clogfile))
{
Int64 fileSizeInBytes = new FileInfo(clogfile).Length;
if (fileSizeInBytes > 5000000)
{
string path = Path.GetFullPath(clogfile);
string filename = Path.GetFileNameWithoutExtension(clogfile);
System.IO.File.Move(clogfile, Path.Combine(path, string.Format("{0}{1}.log", filename, DateTime.Now.ToString("yyyyMMdd_HHmmss"))));
}
}
Move is doing the same = copy and delete old one.
File.Move(#"C:\ScanPDF\Test.pdf", #"C:\BackupPDF\" + string.Format("backup-{0:yyyy-MM-dd_HH:mm:ss}.pdf", DateTime.Now));
// Source file to be renamed
string sourceFile = #"C:\Temp\MaheshChand.jpg";
// Create a FileInfo
System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
// Check if file is there
if (fi.Exists)
{
// Move file with a new name. Hence renamed.
fi.MoveTo(#"C:\Temp\Mahesh.jpg");
Console.WriteLine("File Renamed.");
}
public static class ImageRename
{
public static void ApplyChanges(string fileUrl,
string temporaryImageName,
string permanentImageName)
{
var currentFileName = Path.Combine(fileUrl,
temporaryImageName);
if (!File.Exists(currentFileName))
throw new FileNotFoundException();
var extention = Path.GetExtension(temporaryImageName);
var newFileName = Path.Combine(fileUrl,
$"{permanentImageName}
{extention}");
if (File.Exists(newFileName))
File.Delete(newFileName);
File.Move(currentFileName, newFileName);
}
}
I've encountered a case when I had to rename the file inside the event handler, which was triggering for any file change, including rename, and to skip forever renaming of the file I had to rename it, with:
Making its copy
Removing the original
File.Copy(fileFullPath, destFileName); // Both have the format of "D:\..\..\myFile.ext"
Thread.Sleep(100); // Wait for the OS to unfocus the file
File.Delete(fileFullPath);
private static void Rename_File(string FileFullPath, string NewName) // nes name without directory actualy you can simply rename with fileinfo.MoveTo(Fullpathwithnameandextension);
{
FileInfo fileInfo = new FileInfo(FileFullPath);
string DirectoryRoot = Directory.GetParent(FileFullPath).FullName;
string filecreator = FileFullPath.Substring(DirectoryRoot.Length,FileFullPath.Length-DirectoryRoot.Length);
filecreator = DirectoryRoot + NewName;
try
{
fileInfo.MoveTo(filecreator);
}
catch(Exception ex)
{
Console.WriteLine(filecreator);
Console.WriteLine(ex.Message);
Console.ReadKey();
}
enter code here
// string FileDirectory = Directory.GetDirectoryRoot()
}
When C# doesn't have some feature, I use C++ or C:
public partial class Program
{
[DllImport("msvcrt", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int rename(
[MarshalAs(UnmanagedType.LPStr)]
string oldpath,
[MarshalAs(UnmanagedType.LPStr)]
string newpath);
static void FileRename()
{
while (true)
{
Console.Clear();
Console.Write("Enter a folder name: ");
string dir = Console.ReadLine().Trim('\\') + "\\";
if (string.IsNullOrWhiteSpace(dir))
break;
if (!Directory.Exists(dir))
{
Console.WriteLine("{0} does not exist", dir);
continue;
}
string[] files = Directory.GetFiles(dir, "*.mp3");
for (int i = 0; i < files.Length; i++)
{
string oldName = Path.GetFileName(files[i]);
int pos = oldName.IndexOfAny(new char[] { '0', '1', '2' });
if (pos == 0)
continue;
string newName = oldName.Substring(pos);
int res = rename(files[i], dir + newName);
}
}
Console.WriteLine("\n\t\tPress any key to go to main menu\n");
Console.ReadKey(true);
}
}

how to get the newest created file in a directory using only GetFiles in System.IO Namespace in C#

I would like to create a method which returns me the newest created file in a Directory in C#
with the preferred usage of the Directory.GetFiles() method in the System.IO Namespace. Maybe it's possible to do it also without LINQ to keep it compatible with NET 2.0. Good would be also if the FilePath could be returned as a string not as a File Object if possible
The construct should look like below, but how I can see the newest file only?
public static string NewestFileofDirectory(string DirectoryName)
{
foreach(string File in Directory.GetFiles(DirectoryName))
{
if(new FileInfo(File).CreationDate > ???) //here I stuck
{
//interesting what would be here...
}
}
}
Boilerplate search coming right up. Thank god for LINQ :)
var minTime = DateTime.MinValue;
string theFile = null;
foreach (var entry in Directory.GetFiles(dirName))
{
if (File.GetCreationTimeUtc(entry) > minTime)
{
minTime = File.GetCreationTimeUtc(entry);
theFile = entry;
}
}
You can do this using the FileInfo and DirectoryInfo classes. You will first get all the files in the specified directory and then compare their LastWriteTime to others and thus by comparison you can get the most recently writte or recent file. Here is code for this method.
/// <summary>
/// Returns recently written File from the specified directory.
/// If the directory does not exist or doesn't contain any file, null is returned.
/// </summary>
/// <param name="directoryInfo">Path of the directory that needs to be scanned</param>
/// <returns></returns>
public static string NewestFileofDirectory(string directoryPath )
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
if (directoryInfo == null || !directoryInfo.Exists)
return null;
FileInfo[] files = directoryInfo.GetFiles();
DateTime recentWrite = DateTime.MinValue;
FileInfo recentFile = null;
foreach (FileInfo file in files)
{
if (file.LastWriteTime > recentWrite)
{
recentWrite = file.LastWriteTime;
recentFile = file;
}
}
return recentFile.Name;
}
Both of the above answers have issues with empty directories. Here is a hybrid answer that checks for empty dir and non existent dir.
/// <summary>
/// Returns most recently written Filename from the specified directory.
/// If the directory does not exist or doesn't contain any file, null is returned.
/// </summary>
/// <param name="directoryInfo">Path of the directory that needs to be scanned</param>
/// <param name="filePattern">Search Pattern for file</param>
/// <returns></returns>
public static string NewestFileInDirectory(string directoryPath, string filePattern)
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
if (directoryInfo == null || !directoryInfo.Exists)
{
return null;
}
var minTime = DateTime.MinValue;
string newestFile = null;
foreach (var file in Directory.GetFiles(directoryPath, filePattern))
{
var fileLastWriteTime = File.GetLastWriteTimeUtc(file);
if (fileLastWriteTime > minTime)
{
minTime = fileLastWriteTime;
newestFile = file;
}
}
return newestFile;
}

Rename a file in C#

How do I rename a file using C#?
Take a look at System.IO.File.Move, "move" the file to a new name.
System.IO.File.Move("oldfilename", "newfilename");
System.IO.File.Move(oldNameFullPath, newNameFullPath);
In the File.Move method, this won't overwrite the file if it is already exists. And it will throw an exception.
So we need to check whether the file exists or not.
/* Delete the file if exists, else no exception thrown. */
File.Delete(newFileName); // Delete the existing file if exists
File.Move(oldFileName,newFileName); // Rename the oldFileName into newFileName
Or surround it with a try catch to avoid an exception.
Just add:
namespace System.IO
{
public static class ExtendedMethod
{
public static void Rename(this FileInfo fileInfo, string newName)
{
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
}
}
And then...
FileInfo file = new FileInfo("c:\test.txt");
file.Rename("test2.txt");
You can use File.Move to do it.
First solution
Avoid System.IO.File.Move solutions posted here (marked answer included).
It fails over networks. However, copy/delete pattern works locally and over networks. Follow one of the move solutions, but replace it with Copy instead. Then use File.Delete to delete the original file.
You can create a Rename method to simplify it.
Ease of use
Use the VB assembly in C#.
Add reference to Microsoft.VisualBasic
Then to rename the file:
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName);
Both are strings. Note that myfile has the full path. newName does not.
For example:
a = "C:\whatever\a.txt";
b = "b.txt";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);
The C:\whatever\ folder will now contain b.txt.
You can copy it as a new file and then delete the old one using the System.IO.File class:
if (File.Exists(oldName))
{
File.Copy(oldName, newName, true);
File.Delete(oldName);
}
public void RenameFile(string filePath, string newName)
{
FileInfo fileInfo = new FileInfo(filePath);
fileInfo.MoveTo(fileInfo.Directory.FullName + "\\" + newName);
}
NOTE: In this example code we open a directory and search for PDF files with open and closed parenthesis in the name of the file. You can check and replace any character in the name you like or just specify a whole new name using replace functions.
There are other ways to work from this code to do more elaborate renames but my main intention was to show how to use File.Move to do a batch rename. This worked against 335 PDF files in 180 directories when I ran it on my laptop. This is spur of the moment code and there are more elaborate ways to do it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BatchRenamer
{
class Program
{
static void Main(string[] args)
{
var dirnames = Directory.GetDirectories(#"C:\the full directory path of files to rename goes here");
int i = 0;
try
{
foreach (var dir in dirnames)
{
var fnames = Directory.GetFiles(dir, "*.pdf").Select(Path.GetFileName);
DirectoryInfo d = new DirectoryInfo(dir);
FileInfo[] finfo = d.GetFiles("*.pdf");
foreach (var f in fnames)
{
i++;
Console.WriteLine("The number of the file being renamed is: {0}", i);
if (!File.Exists(Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", ""))))
{
File.Move(Path.Combine(dir, f), Path.Combine(dir, f.ToString().Replace("(", "").Replace(")", "")));
}
else
{
Console.WriteLine("The file you are attempting to rename already exists! The file path is {0}.", dir);
foreach (FileInfo fi in finfo)
{
Console.WriteLine("The file modify date is: {0} ", File.GetLastWriteTime(dir));
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
None of the answers mention writing a unit testable solution. You could use System.IO.Abstractions as it provides a testable wrapper around FileSystem operations, using which you can create a mocked file system objects and write unit tests.
using System.IO.Abstractions;
IFileInfo fileInfo = _fileSystem.FileInfo.FromFileName("filePathAndName");
fileInfo.MoveTo(Path.Combine(fileInfo.DirectoryName, newName));
It was tested, and it is working code to rename a file.
Use:
using System.IO;
string oldFilePath = #"C:\OldFile.txt"; // Full path of old file
string newFilePath = #"C:\NewFile.txt"; // Full path of new file
if (File.Exists(newFilePath))
{
File.Delete(newFilePath);
}
File.Move(oldFilePath, newFilePath);
Use:
public static class FileInfoExtensions
{
/// <summary>
/// Behavior when a new filename exists.
/// </summary>
public enum FileExistBehavior
{
/// <summary>
/// None: throw IOException "The destination file already exists."
/// </summary>
None = 0,
/// <summary>
/// Replace: replace the file in the destination.
/// </summary>
Replace = 1,
/// <summary>
/// Skip: skip this file.
/// </summary>
Skip = 2,
/// <summary>
/// Rename: rename the file (like a window behavior)
/// </summary>
Rename = 3
}
/// <summary>
/// Rename the file.
/// </summary>
/// <param name="fileInfo">the target file.</param>
/// <param name="newFileName">new filename with extension.</param>
/// <param name="fileExistBehavior">behavior when new filename is exist.</param>
public static void Rename(this System.IO.FileInfo fileInfo, string newFileName, FileExistBehavior fileExistBehavior = FileExistBehavior.None)
{
string newFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(newFileName);
string newFileNameExtension = System.IO.Path.GetExtension(newFileName);
string newFilePath = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileName);
if (System.IO.File.Exists(newFilePath))
{
switch (fileExistBehavior)
{
case FileExistBehavior.None:
throw new System.IO.IOException("The destination file already exists.");
case FileExistBehavior.Replace:
System.IO.File.Delete(newFilePath);
break;
case FileExistBehavior.Rename:
int dupplicate_count = 0;
string newFileNameWithDupplicateIndex;
string newFilePathWithDupplicateIndex;
do
{
dupplicate_count++;
newFileNameWithDupplicateIndex = newFileNameWithoutExtension + " (" + dupplicate_count + ")" + newFileNameExtension;
newFilePathWithDupplicateIndex = System.IO.Path.Combine(fileInfo.Directory.FullName, newFileNameWithDupplicateIndex);
}
while (System.IO.File.Exists(newFilePathWithDupplicateIndex));
newFilePath = newFilePathWithDupplicateIndex;
break;
case FileExistBehavior.Skip:
return;
}
}
System.IO.File.Move(fileInfo.FullName, newFilePath);
}
}
How to use this code
class Program
{
static void Main(string[] args)
{
string targetFile = System.IO.Path.Combine(#"D://test", "New Text Document.txt");
string newFileName = "Foo.txt";
// Full pattern
System.IO.FileInfo fileInfo = new System.IO.FileInfo(targetFile);
fileInfo.Rename(newFileName);
// Or short form
new System.IO.FileInfo(targetFile).Rename(newFileName);
}
}
I couldn't find an approach which suits me, so I propose my version. Of course, it needs input and error handling.
public void Rename(string filePath, string newFileName)
{
var newFilePath = Path.Combine(Path.GetDirectoryName(filePath), newFileName + Path.GetExtension(filePath));
System.IO.File.Move(filePath, newFilePath);
}
In my case, I want the name of the renamed file to be unique, so I add a date-time stamp to the name. This way, the filename of the 'old' log is always unique:
if (File.Exists(clogfile))
{
Int64 fileSizeInBytes = new FileInfo(clogfile).Length;
if (fileSizeInBytes > 5000000)
{
string path = Path.GetFullPath(clogfile);
string filename = Path.GetFileNameWithoutExtension(clogfile);
System.IO.File.Move(clogfile, Path.Combine(path, string.Format("{0}{1}.log", filename, DateTime.Now.ToString("yyyyMMdd_HHmmss"))));
}
}
Move is doing the same = copy and delete old one.
File.Move(#"C:\ScanPDF\Test.pdf", #"C:\BackupPDF\" + string.Format("backup-{0:yyyy-MM-dd_HH:mm:ss}.pdf", DateTime.Now));
// Source file to be renamed
string sourceFile = #"C:\Temp\MaheshChand.jpg";
// Create a FileInfo
System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
// Check if file is there
if (fi.Exists)
{
// Move file with a new name. Hence renamed.
fi.MoveTo(#"C:\Temp\Mahesh.jpg");
Console.WriteLine("File Renamed.");
}
public static class ImageRename
{
public static void ApplyChanges(string fileUrl,
string temporaryImageName,
string permanentImageName)
{
var currentFileName = Path.Combine(fileUrl,
temporaryImageName);
if (!File.Exists(currentFileName))
throw new FileNotFoundException();
var extention = Path.GetExtension(temporaryImageName);
var newFileName = Path.Combine(fileUrl,
$"{permanentImageName}
{extention}");
if (File.Exists(newFileName))
File.Delete(newFileName);
File.Move(currentFileName, newFileName);
}
}
I've encountered a case when I had to rename the file inside the event handler, which was triggering for any file change, including rename, and to skip forever renaming of the file I had to rename it, with:
Making its copy
Removing the original
File.Copy(fileFullPath, destFileName); // Both have the format of "D:\..\..\myFile.ext"
Thread.Sleep(100); // Wait for the OS to unfocus the file
File.Delete(fileFullPath);
private static void Rename_File(string FileFullPath, string NewName) // nes name without directory actualy you can simply rename with fileinfo.MoveTo(Fullpathwithnameandextension);
{
FileInfo fileInfo = new FileInfo(FileFullPath);
string DirectoryRoot = Directory.GetParent(FileFullPath).FullName;
string filecreator = FileFullPath.Substring(DirectoryRoot.Length,FileFullPath.Length-DirectoryRoot.Length);
filecreator = DirectoryRoot + NewName;
try
{
fileInfo.MoveTo(filecreator);
}
catch(Exception ex)
{
Console.WriteLine(filecreator);
Console.WriteLine(ex.Message);
Console.ReadKey();
}
enter code here
// string FileDirectory = Directory.GetDirectoryRoot()
}
When C# doesn't have some feature, I use C++ or C:
public partial class Program
{
[DllImport("msvcrt", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int rename(
[MarshalAs(UnmanagedType.LPStr)]
string oldpath,
[MarshalAs(UnmanagedType.LPStr)]
string newpath);
static void FileRename()
{
while (true)
{
Console.Clear();
Console.Write("Enter a folder name: ");
string dir = Console.ReadLine().Trim('\\') + "\\";
if (string.IsNullOrWhiteSpace(dir))
break;
if (!Directory.Exists(dir))
{
Console.WriteLine("{0} does not exist", dir);
continue;
}
string[] files = Directory.GetFiles(dir, "*.mp3");
for (int i = 0; i < files.Length; i++)
{
string oldName = Path.GetFileName(files[i]);
int pos = oldName.IndexOfAny(new char[] { '0', '1', '2' });
if (pos == 0)
continue;
string newName = oldName.Substring(pos);
int res = rename(files[i], dir + newName);
}
}
Console.WriteLine("\n\t\tPress any key to go to main menu\n");
Console.ReadKey(true);
}
}

Categories