How can I get the line number which threw exception? - c#

In a catch block, how can I get the line number which threw an exception?

If you need the line number for more than just the formatted stack trace you get from Exception.StackTrace, you can use the StackTrace class:
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}
Note that this will only work if there is a pdb file available for the assembly.

Simple way, use the Exception.ToString() function, it will return the line after the exception description.
You can also check the program debug database as it contains debug info/logs about the whole application.

If you don't have the .PBO file:
C#
public int GetLineNumber(Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
Vb.net
Public Function GetLineNumber(ByVal ex As Exception)
Dim lineNumber As Int32 = 0
Const lineSearch As String = ":line "
Dim index = ex.StackTrace.LastIndexOf(lineSearch)
If index <> -1 Then
Dim lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length)
If Int32.TryParse(lineNumberText, lineNumber) Then
End If
End If
Return lineNumber
End Function
Or as an extentions on the Exception class
public static class MyExtensions
{
public static int LineNumber(this Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
}

You could include .PDB symbol files associated to the assembly which contain metadata information and when an exception is thrown it will contain full information in the stacktrace of where this exception originated. It will contain line numbers of each method in the stack.

Check this one
StackTrace st = new StackTrace(ex, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0);
//Get the file name
string fileName = frame.GetFileName();
//Get the method name
string methodName = frame.GetMethod().Name;
//Get the line number from the stack frame
int line = frame.GetFileLineNumber();
//Get the column number
int col = frame.GetFileColumnNumber();

I added an extension to Exception which returns the line, column, method, filename and message:
public static class Extensions
{
public static string ExceptionInfo(this Exception exception)
{
StackFrame stackFrame = (new StackTrace(exception, true)).GetFrame(0);
return string.Format("At line {0} column {1} in {2}: {3} {4}{3}{5} ",
stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(),
stackFrame.GetMethod(), Environment.NewLine, stackFrame.GetFileName(),
exception.Message);
}
}

Convert.ToInt32(ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(' ')));
This will give the Exception line no.

I tried using the solution By #davy-c but had an Exception "System.FormatException: 'Input string was not in a correct format.'", this was due to there still being text past the line number, I modified the code he posted and came up with:
int line = Convert.ToInt32(objErr.ToString().Substring(objErr.ToString().IndexOf("line")).Substring(0, objErr.ToString().Substring(objErr.ToString().IndexOf("line")).ToString().IndexOf("\r\n")).Replace("line ", ""));
This works for me in VS2017 C#.

Update to the answer
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(st.FrameCount-1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();

Extension Method
static class ExceptionHelpers
{
public static int LineNumber(this Exception ex)
{
int n;
int i = ex.StackTrace.LastIndexOf(" ");
if (i > -1)
{
string s = ex.StackTrace.Substring(i + 1);
if (int.TryParse(s, out n))
return n;
}
return -1;
}
}
Usage
try
{
throw new Exception("A new error happened");
}
catch (Exception ex)
{
//If error in exception LineNumber() will be -1
System.Diagnostics.Debug.WriteLine("[" + ex.LineNumber() + "] " + ex.Message);
}

Working for me:
var st = new StackTrace(e, true);
// Get the bottom stack frame
var frame = st.GetFrame(st.FrameCount - 1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
var method = frame.GetMethod().ReflectedType.FullName;
var path = frame.GetFileName();

Line numbers will be included in the stack trace if the library which generated the exception is compiled with debug symbols. This can be a separate file (*.pdb) or embedded in the library.
For .NET Core, .NET 5 and later, to have full exception line numbers in release builds, configure the project as follows:
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<!-- Only enable the following if the line numbers mismatch -->
<!--<Optimize>false</Optimize>-->
<!--
Additional properties which may impact how printed line numbers match the source code line numbers are listed here:
https://learn.microsoft.com/en-us/dotnet/core/run-time-config/compilation
-->
</PropertyGroup>
The above configuration will include debug symbols directly with the built files, which can be published as nugets.
An alternative to the above is to restore debug packages together with the main nuget packages, which is currently not yet supported: https://github.com/NuGet/Home/issues/9667
Now get the exception line numbers:
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}

If your stack trace is bigger than one it should be:
var st = new StackTrace(es, true);
// Get the top stack frame
var frame = st.GetFrame(st.FrameCount - 1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();

protected void Page_Load(object sender, EventArgs e)
{
try
{
int line1 = 1;
int line2 =int.Parse("Test");
}
catch (Exception ex)
{
var st =new System.Diagnostics.StackTrace(ex,true);
var frame = st.GetFrame(st.FrameCount - 1);
var linenumber = frame.GetFileLineNumber();
}
}

In Global.resx file there is an event called Application_Error
it fires whenever an error occurs,,you can easily get any information about the error,and send it to a bug tracking e-mail.
Also i think all u need to do is to compile the global.resx and add its dll's (2 dlls) to your bin folder and it will work!

Related

"Illegal characters in path" error opening service config file

I am trying to read a section of a Windows service configuration file and extract a value (port number). Following code works fine on my local machine but not in any server I tested even though the service is installed in exact same folder structure.
On servers, I get "illegal character in path" error (I added a couple of try-catch to see where it dies and what the message was).
public static string GetCurrentTCPPort()
{
string sTCPPort = "7899";
string ServiceName = "IDC - Tcp Interface Service";
using (ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + ServiceName + "'"))
{
try
{
wmiService.Get();
}
catch (Exception ex)
{
throw new Exception("Died in GetCurrentTCPPort - wmiService.Get()");
}
string ServiceExePath = wmiService["PathName"].ToString();
System.Configuration.Configuration config;
try
{
config = ConfigurationManager.OpenExeConfiguration(ServiceExePath); // FilePath = "C:\\IDC\\APP\\IDC - Tcp Interface Service\\IDC.Service.TcpInterface.exe.config"
string[] saLiteningIPs = config.AppSettings.Settings["TcpServerListenIpAddrsAndPortNos"].Value.Split(','); // "1.2.3.4:7899,1.2.3.5:7899"
if (saLiteningIPs.Length > 0)
{
sTCPPort = saLiteningIPs[0].Split(':')[1];
}
}
catch (Exception ex)
{ // This exception is thrown
string sExcep = string.Format("Died in GetCurrentTCPPort - OpenExeConfiguration(); ServiceExePath: {0}{1}Exception: {2}", ServiceExePath, Environment.NewLine, ex.Message);
throw new Exception(sExcep);
}
}
return sTCPPort;
}
When I run it, I get:
Died in GetCurrentTCPPort - OpenExeConfiguration(); currentserviceExePath: "C:\IDC\APP\IDC - Tcp Interface Service\IDC.Service.TcpInterface.exe"
Exception: An error occurred loading a configuration file: Illegal characters in path.
Screen shot of config file location:
Update - With the fix
public static string GetCurrentTCPPort()
{
string sTCPPort = "7899";
string ServiceName = "IDC - Tcp Interface Service";
using (ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + ServiceName + "'"))
{
try
{
wmiService.Get();
}
catch (Exception ex)
{
throw new Exception("Died in GetCurrentTCPPort - wmiService.Get()");
}
string ServiceExePath = wmiService["PathName"].ToString();
// Added below two lines to fix the issue
List<char> invalidPathChars = Path.GetInvalidPathChars().ToList();
invalidPathChars.ForEach(c => ServiceExePath = ServiceExePath.Replace(c.ToString(), String.Empty));
System.Configuration.Configuration config;
try
{
config = ConfigurationManager.OpenExeConfiguration(ServiceExePath);
string[] saLiteningIPs = config.AppSettings.Settings["TcpServerListenIpAddrsAndPortNos"].Value.Split(',');
if (saLiteningIPs.Length > 0)
{
sTCPPort = saLiteningIPs[0].Split(':')[1];
}
}
catch (Exception ex)
{ // This exception is thrown
string sExcep = string.Format("Died in GetCurrentTCPPort - OpenExeConfiguration(); ServiceExePath: {0}{1}Exception: {2}", ServiceExePath, Environment.NewLine, ex.Message);
throw new Exception(sExcep);
}
}
return sTCPPort;
}
I am not sure why this happens when application is deployed to server and not when I run it locally, even though folder structure and files are exactly same on both; maybe there are invisible characters that are caught on the server but not locally.
Nonetheless, it seems the following modification fixed the issue, in case anyone else runs into this problem.
Right below the line getting the exe path, added these two lines:
string ServiceExePath = wmiService["PathName"].ToString();
List<char> invalidPathChars = Path.GetInvalidPathChars().ToList();
invalidPathChars.ForEach(c => ServiceExePath = ServiceExePath.Replace(c.ToString(), String.Empty));
I have also updated the the question.

C# try and catch

I've just signed up to the website so I have probably put this wrong. Anyways, I am trying to use the try and catch in C# to catch my file if it not found. Here is my code for it at the moment. To repeat myself, I want the program to read the file in, as it does- but then if the file is not found, I would like it to give an error saying "cannot find the file" or something along those lines, not simply just crash. (I've just started learning C#)
Thanks for the help!
string file = #"C:\Users\Henry\Desktop\Assessments\Programming and data structures\grades_multiple.txt";
try
{
file = #"C:\Users\Henry\Desktop\Assessments\Programming and data structures\grades_multiple.txt";
}
catch
{
Console.WriteLine("Could not find the file - grades_multiple.txt");
}
//ARRAY for string lines
string[] Line = new string[6];
Line[0] = File.ReadLines(file).Skip(1).Take(1).First();
You should read the file inside the try catch and catch FileNotFoundException, like this:
var file = #"C:\Users\Henry\Desktop\Assessments\Programming and data structures\grades_multiple.txt";
string[] lines;
try
{
lines = File.ReadAllLines(file);
}
catch (FileNotFoundException exnotfound)
{
// file not found exception
}
catch (Exception ex)
{
// handle other exceptions
}
You need to put the code that is error prone inside of try block.
try
{
Line[0] = File.ReadLines(file).Skip(1).Take(1).First();
}
catch(Exception ex)
{
Console.WriteLine("Could not find the file - grades_multiple.txt");
}
BTW, you can handle this situation by checking if file exists first using File.Exists method.There is no need to catch exception for that.
Try catch just doesn't work like this.
You are trying to catch a line of code that changes a string and doesn't change a file, so the file doesn't need to exist, so it will never throw an exception (in your case), and it will never catch it.
You should surround code that can go wrong: File.ReadLines
Your code will become like this:
string file = #"C:\Users\Henry\Desktop\Assessments\Programming and data structures\grades_multiple.txt";
//can go wrong
try
{
//ARRAY for string lines
string[] Line = new string[6];
Line[0] = File.ReadLines(file).Skip(1).Take(1).First();
}
//File.ReadLines is null
catch
{
Console.WriteLine("Could not find the file - grades_multiple.txt");
}
I also think it is even better practice to check it with an if statement, instead of catching it when it goes wrong:
//if file exists
if(File.Exists(file))
{
//ARRAY for string lines
string[] Line = new string[6];
Line[0] = File.ReadLines(file).Skip(1).Take(1).First();
}
//File does not exist
else
{
Console.WriteLine("Could not find the file - grades_multiple.txt");
}
When ever possible you should try to avoid throwing exceptions just to display a message to the user or for conditions that can be easily tested. This is primarily a performance consideration as throwing an exception is expensive. Additionally performance can be impacted by loading the entire file into memory unless you know that the file is relatively small..
Here is a quick and raw example on how you may test for the condition and handle it.
void Main()
{
var filePath ="C:\\TEST.DAT";
if(!File.Exists(filePath)){ DisplayFileNotFoundError(filePath); }
try
{
var lines = GetFileLines(filePath);
if(lines == null) { DisplayFileNotFoundError(filePath);}
// do work with lines;
}
catch (Exception ex)
{
DisplayFileReadException(ex);
}
}
void DisplayErrorMessageToUser(string filePath)
{
Console.WriteLine("The file does not exist");
}
void DisplayFileReadException(Exception ex){
Console.WriteLine(ex.Message);
}
string[] GetFileLines(string filePath){
if(!File.Exists(filePath)){ return null; }
string[] lines;
try
{
lines = File.ReadLines(filePath);
return lines;
}
catch (FileNotFoundException fnf){
Trace.WriteLine(fnf.Message);
return null;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
throw ex;
}
}
Performance Test, FileNotFoundException vs File.Exists
void Main()
{
int max = 100000;
long[] feSampling = new long[max];
long[] exSampling = new long[max];
String pathRoot ="C:\\MISSINGFILE.TXT";
String path = null;
Stopwatch sw = new Stopwatch();
for (int i = 0; i < max; i++)
{
path = pathRoot + i.ToString();
sw.Start();
File.Exists(pathRoot);
sw.Stop();
feSampling[i] = sw.ElapsedTicks;
sw.Reset();
}
StreamReader sr = null;
sw.Reset();
for (int i = 0; i < max; i++)
{
path = pathRoot + i.ToString();
try
{
sw.Start();
sr = File.OpenText(path);
}
catch (FileNotFoundException)
{
sw.Stop();
exSampling[i] = sw.ElapsedTicks;
sw.Reset();
if(sr != null) { sr.Dispose();}
}
}
Console.WriteLine("Total Samplings Per Case: {0}", max);
Console.WriteLine("File.Exists (Ticsk) - Min: {0}, Max: {1}, Mean: {2}", feSampling.Min(), feSampling.Max(), feSampling.Average ());
Console.WriteLine("FileNotFoundException (Ticks) - Min: {0}, Max: {1}, Mean: {2}", exSampling.Min(), exSampling.Max(), exSampling.Average ());
}

Reading from a file in by specifying start point and end point

I want to read from an input file in C#. Below is my code.
public string ReadFromNewEntityFile()
{
string template=null;
StringBuilder s = new StringBuilder();
//char[] sourcesystemhost=null;
string inputFileName = ConfigurationManager.AppSettings["inputNewEntityFilePath"].ToString();
System.IO.StreamReader myFile;
try
{
myFile = new System.IO.StreamReader(inputFileName);
myFile.ReadLine();
while ((template = myFile.ReadLine()) != "[[END SourceSystemHost]]")
{
s.AppendLine(template);
}
}
catch (Exception ex)
{
log.Error("In Filehandler class :" + ex.Message);
throw new Exception("Input file not read" + ex.Message);
}
return template;
}
The problem is want to specify the starting point and end point for reading the contents. Here I am able to specify only the end point. How can i specify the starting point?
Please help
Assuming your start/end "points" are actually lines, you basically need to read from the start and skip the lines until you reach the right one. Here's an easy way of doing it using File.ReadLines:
var lines = File.ReadLines(inputFileName)
.SkipWhile(line => line != "[[START SourceSystemHost]]")
.Skip(1) // Skip the intro line
.TakeWhile(line => line != "[[END SourceSystemHost]]");
You could use File.ReadLines which does the same but more readable. Then use LINQ to find your start- and end-points:
var range = File.ReadLines(inputFileName)
.SkipWhile(l => !l.TrimStart().StartsWith("[[Start SourceSystemHost]]"))
.TakeWhile(l => !l.TrimStart().StartsWith("[[END SourceSystemHost]]"));
string result = string.Join(Environment.NewLine, range);

"The process cannot access the file because it is being used by another process"

The full error I am receiving is:
"The process cannot access the file 'e:\Batch\NW\data_Test\IM_0232\input\RN318301.WM' because it is being used by another process.>>> at IM_0232.BatchModules.BundleSort(String bundleFileName)
at IM_0232.BatchModules.ExecuteBatchProcess()"
The involved code can be seen below. The RN318301.WM file being processed is a text file that contains information which will eventually be placed in PDF documents. There are many documents referenced in the RN318301.WM text file with each one being represented by a collection of rows. As can be seen in the code, the RN318301.WM text file is first parsed to determine the number of documents represented in it as well as the maximum number of lines in a documents. This information is then used to create two-dimensional array that will contain all of the document information. The RN318301.WM text file is parsed again to populate the two-dimensional array and at the same time information is collected into a dictionary that will be sorted later in the routine.
The failure occurs at the last line below:
File.Delete(_bundlePath + Path.GetFileName(bundleFileName));
This is a sporadic problem that occurs only rarely. It has even been seen to occur with a particular text file with which it had not previously occurred. That is, a particular text file will process fine but then on reprocessing the error will be triggered.
Can anyone help us to diagnose the cause of this error? Thank you very much...
public void BundleSort(string bundleFileName)
{
Dictionary<int, string> memberDict = new Dictionary<int, string>();
Dictionary<int, string> sortedMemberDict = new Dictionary<int, string>();
//int EOBPosition = 0;
int EOBPosition = -1;
int lineInEOB = 0;
int eobCount = 0;
int lineCount = 0;
int maxLineCount = 0;
string compareString;
string EOBLine;
//#string[][] EOBLineArray;
string[,] EOBLineArray;
try
{
_batch.TranLog_Write("\tBeginning sort of bundle " + _bundleInfo.BundleName + " to facilitate householding");
//Read the bundle and create a dictionary of comparison strings with EOB position in the bundle being the key
StreamReader file = new StreamReader(#_bundlePath + _bundleInfo.BundleName);
//The next section of code counts CH records as well as the maximum number of CD records in an EOB. This information is needed for initialization of the 2-dimensional EOBLineArray array.
while ((EOBLine = file.ReadLine()) != null)
{
if (EOBLine.Substring(0, 2) == "CH" || EOBLine.Substring(0, 2) == "CT")
{
if (lineCount == 0)
lineCount++;
if (lineCount > maxLineCount)
{
maxLineCount = lineCount;
}
eobCount++;
if (lineCount != 1)
lineCount = 0;
}
if (EOBLine.Substring(0, 2) == "CD")
{
lineCount++;
}
}
EOBLineArray = new string[eobCount, maxLineCount + 2];
file = new StreamReader(#_bundlePath + _bundleInfo.BundleName);
try
{
while ((EOBLine = file.ReadLine()) != null)
{
if (EOBLine.Substring(0, 2) == "CH")
{
EOBPosition++;
lineInEOB = 0;
compareString = EOBLine.Substring(8, 40).Trim() + EOBLine.Substring(49, 49).TrimEnd().TrimStart() + EOBLine.Substring(120, 5).TrimEnd().TrimStart();
memberDict.Add(EOBPosition, compareString);
EOBLineArray[EOBPosition, lineInEOB] = EOBLine;
}
else
{
if (EOBLine.Substring(0, 2) == "CT")
{
EOBPosition++;
EOBLineArray[EOBPosition, lineInEOB] = EOBLine;
}
else
{
lineInEOB++;
EOBLineArray[EOBPosition, lineInEOB] = EOBLine;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
_batch.TranLog_Write("\tSending original unsorted bundle to archive");
if(!(File.Exists(_archiveDir + "\\" +DateTime.Now.ToString("yyyyMMdd")+ Path.GetFileName(bundleFileName) + "_original")))
{
File.Copy(_bundlePath + Path.GetFileName(bundleFileName), _archiveDir + "\\" +DateTime.Now.ToString("yyyyMMdd")+ Path.GetFileName(bundleFileName) + "_original");
}
file.Close();
file.Dispose();
GC.Collect();
File.Delete(_bundlePath + Path.GetFileName(bundleFileName));
You didn't close/dispose your StreamReader first time round so the file handle is still open
Consider using the using construct - this will automatically dispose of the object when it goes out of scope:
using(var file = new StreamReader(args))
{
// Do stuff
}
// file has now been disposed/closed etc
You need to close your StreamReaders for one thing.
StreamReader file = new StreamReader(#_bundlePath + _bundleInfo.BundleName);
You need to close the StreamReader object, and you could do this in a finally block:
finally {
file.Close();
}
A better way is to use a using block:
using (StreamReader file = new StreamReader(#_bundlePath + _bundleInfo.BundleName)) {
...
}
It looks to me like you are calling GC.Collect to try to force the closing of these StreamReaders, but that doesn't guarantee that they will be closed immediately as per the MSDN doc:
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx
From that doc:
"All objects, regardless of how long they have been in memory, are considered for collection;"

Move files in C#

I am moving some images (filenames are(1).PNG, (2).PNG and so on) from one directory to another. I am using the following code:
for (int i = 1; i < n; i++)
{
try
{
from = "E:\\vid\\(" + i + ").PNG";
to = "E:\\ConvertedFiles\\" + i + ".png";
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
However, I am getting the following error:
A first chance exception of type System.IO.FileNotFoundException occurred in mscorlib.dll
System.IO.FileNotFoundException: Could not find file 'E:\vid\(1).PNG'.
Also, I am planning to rename the files so that the converted file name will be 00001.png, 00002.png, ... 00101.png and so on.
I suggest you to use '#' in order to escape slashes in a more readable way. Use also Path.Combine(...) in order to concatenate paths and PadLeft in order to have your filenames as your specifics.
for (int i = 1; i < n; i++)
{
try
{
from = System.IO.Path.Combine(#"E:\vid\","(" + i.ToString() + ").PNG");
to = System.IO.Path.Combine(#"E:\ConvertedFiles\",i.ToString().PadLeft(6,'0') + ".png");
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
Why don't you use something like this?
var folder = new DirectoryInfo(#"E:\vid\"));
if (folder.Exists)
{
var files = folder.GetFiles(".png");
files.toList().ForEach(f=>File.Move(from,to));
}
The exception means that the file E:\vid(1).PNG doesn't exist. Do you mean E:\vid1.PNG?
Use System.IO.Path class for constructing paths, it's better than concatenating strings. You don't have to worry about escapting backslashes.
I just ran this in Visual Studio. It worked.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
int n = 3;
for (int i = 1; i < n; i++)
{
string from = "C:\\vid\\(" + i + ").PNG";
string to = "C:\\ConvertedFiles\\" + i + ".png";
{
try
{
File.Move(from, to); // Try to move
Console.WriteLine("Moved"); // Success
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine(e); // Write error
}
}
}
}
}
}
Maybe when you were moving files into vid directory to begin the test, windows shaved off the parenthesis. (1).png became 1.png... I got a file not found error from that phenomenon... otherwise, your code is solid. My version is almost identical.
i.ToString()
might help you. You are passing
from = "E:\\vid\\(" + i + ").PNG";
to = "E:\\ConvertedFiles\\" + i + ".png";
I as integer and concatenation doesn't work due to that
and instead of using \\, add # like this
from = #"E:\vid\(" + i + ").PNG";
var folder = new DirectoryInfo(sourcefolder);
if (folder.Exists)
{
var files = folder.GetFiles("*.png");
files.ToList().ForEach(f => File.Move(sourcefolder + f, newFolderName + f));
}
I believe this will help.

Categories

Resources