C
C#•2y ago
YellowDuck

Operators.ConcatenateObject()

Looking for a replacement for the method. i c# This Method comes from vb https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.compilerservices.operators.concatenateobject?view=net-7.0 But is there something else I can use?
Operators.ConcatenateObject(Object, Object) Method (Microsoft.Visua...
Represents the Visual Basic concatenation (&) operator.
34 Replies
Brady Kelly
Brady Kelly•2y ago
Why do you want to concatenate objects? That makes little sense Then they want to concat strings, you're right. For objects it would only make sense in a language like JavaScript where doing so would concat 2 associative arrays
Brady Kelly
Brady Kelly•2y ago
Oh man, framework source is damn ugly in VB
YellowDuck
YellowDuck•2y ago
Hi, Got an old project in my lap at work that my boss wants me to rebuild with newer technology. He means write it in csharp and not in vb haha. I want to Check lastActivity folder and file name
sLastActivity = "Create object oFso"
Set oFso = CreateObject("Scripting.FileSystemObject")

'Search certificate source folder for pdf files and archive files
Set oFolder = oFso.GetFolder(sReportSourceRootFolder)
Set oFiles = oFolder.Files
For Each oFile In oFiles 'Take all pdf-files first
sLastActivity = "report " & oFile.Name
'Pdf-files: copy pdf and archive
If oFile.DateLastModified < (Now() - 0.05) And LCase(oFso.GetExtensionName(oFile.Path)) = "pdf" And Not Left$(oFile.Name, 1) = "~" Then
sLog = sLog & vbCrLf & oFile.Name & " Rep"
If CopyToMioArchive(oFile.Path) Then Archive oFile.Path, sReportDestRootFolder
End If
Next
sLastActivity = "Create object oFso"
Set oFso = CreateObject("Scripting.FileSystemObject")

'Search certificate source folder for pdf files and archive files
Set oFolder = oFso.GetFolder(sReportSourceRootFolder)
Set oFiles = oFolder.Files
For Each oFile In oFiles 'Take all pdf-files first
sLastActivity = "report " & oFile.Name
'Pdf-files: copy pdf and archive
If oFile.DateLastModified < (Now() - 0.05) And LCase(oFso.GetExtensionName(oFile.Path)) = "pdf" And Not Left$(oFile.Name, 1) = "~" Then
sLog = sLog & vbCrLf & oFile.Name & " Rep"
If CopyToMioArchive(oFile.Path) Then Archive oFile.Path, sReportDestRootFolder
End If
Next
trying to convert this to c# 🙂
YellowDuck
YellowDuck•2y ago
Set oFiles = oFolder.Files = LastActivity = Operators.ConcatenateObject("file", File); But then I need to install And I do not what to do that
YellowDuck
YellowDuck•2y ago
Can I write it like this?
LastActivity = String.Concat("report", File);
LastActivity = String.Concat("report", File);
dancepanda42
dancepanda42•2y ago
how should last activity look like? "report" and the file name or the complete file path?
YellowDuck
YellowDuck•2y ago
Just the report and the filename. I have the filepath It is a automating file archive system. It looks for three folders and looks for files that are 1 hour and moves the files to a other folder like a Archive.
dancepanda42
dancepanda42•2y ago
var folders = new string[]
{
"Path\\to\\first\\folder",
"Path\\to\\second\\folder",
"Path\\to\\third\\folder",
};

foreach(var file in folders.SelectMany(x => Directory.GetFiles(x)))
{
var fileInfo = new FileInfo(file);
if (fileInfo.Extension == ".pdf" &&
fileInfo.LastWriteTime < DateTime.Now - TimeSpan.FromMilliseconds(500) &&
!file.StartsWith("~"))
{

// Do your stuff here...
LastActivity = $"report{fileInfo.Name}";
}
}
var folders = new string[]
{
"Path\\to\\first\\folder",
"Path\\to\\second\\folder",
"Path\\to\\third\\folder",
};

foreach(var file in folders.SelectMany(x => Directory.GetFiles(x)))
{
var fileInfo = new FileInfo(file);
if (fileInfo.Extension == ".pdf" &&
fileInfo.LastWriteTime < DateTime.Now - TimeSpan.FromMilliseconds(500) &&
!file.StartsWith("~"))
{

// Do your stuff here...
LastActivity = $"report{fileInfo.Name}";
}
}
here a little code snipped how it could work... (not tested)
YellowDuck
YellowDuck•2y ago
Nice, Looks like the one I am write right now but yours was so much better 🙂 Thank you for you help.
dancepanda42
dancepanda42•2y ago
no problem in C# there is also a lot of code sugar
dancepanda42
dancepanda42•2y ago
this line
LastActivity = $"report{fileInfo.Name}";
LastActivity = $"report{fileInfo.Name}";
just call
LastActivity = string.Concat("report", fileInfo.Name);
LastActivity = string.Concat("report", fileInfo.Name);
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxTAbgIYBO6AZgJYA2AUwCSAOy4B7dAF50IgQHdMcAHRCA8soBi/YWPHUARAdoBuRkyJtOAGVYBnAC4BBAMYOezHg4Ce09ABIDdgEAB3F2BzxeQVEJZQA5VgBbAQBfAzMkJlTkVKA
SharpLab
C#/VB/F# compiler playground.
Brady Kelly
Brady Kelly•2y ago
Shouldn't you be using Path.Combind instead of interpolation? Or is "report" part of the filename?
dancepanda42
dancepanda42•2y ago
As I see it the variable LastActivity is only used to display the current process and is not used for file processing. Otherwise you are right, Path.Join or Path.Combine would be the right choice.
Brady Kelly
Brady Kelly•2y ago
OK
YellowDuck
YellowDuck•2y ago
This is my result
string[] AttachmentFiles = Directory.GetFiles(ReportSourceRootFolder, "*.pdf", SearchOption.AllDirectories);
LastActivity = String.Concat("report", AttachmentFiles);
foreach (string AttachmentFile in AttachmentFiles)
{
var fileInfo = new FileInfo(AttachmentFile);
if (File.GetLastWriteTime(AttachmentFile) < DateTime.Now - TimeSpan.FromMilliseconds(500) && fileInfo.Extension == ".pdf" && !AttachmentFile.StartsWith("~"))
{
Archive(AttachmentFile, AttachmentDestRootFolder);
}
}
string[] AttachmentFiles = Directory.GetFiles(ReportSourceRootFolder, "*.pdf", SearchOption.AllDirectories);
LastActivity = String.Concat("report", AttachmentFiles);
foreach (string AttachmentFile in AttachmentFiles)
{
var fileInfo = new FileInfo(AttachmentFile);
if (File.GetLastWriteTime(AttachmentFile) < DateTime.Now - TimeSpan.FromMilliseconds(500) && fileInfo.Extension == ".pdf" && !AttachmentFile.StartsWith("~"))
{
Archive(AttachmentFile, AttachmentDestRootFolder);
}
}
How come my code snippes do not look like yours @dancepanda42 ?
dancepanda42
dancepanda42•2y ago
There are many ways that lead to the target. You can omit the query
fileInfo.Extension == ".pdf"
fileInfo.Extension == ".pdf"
because of the filter in
Directory.GetFiles(ReportSourceRootFolder, "*.pdf", SearchOption.AllDirectories);
Directory.GetFiles(ReportSourceRootFolder, "*.pdf", SearchOption.AllDirectories);
. What is the variable LastActivity needed for anyway? behind the three "`" comes a csharp
YellowDuck
YellowDuck•2y ago
Its for the writing in the logging file later in the code.
LastActivity = "Writing log file";
Log = Log + Environment.NewLine + "RoboArchiveManager finished at " + DateTime.Now.ToString() + Environment.NewLine + Environment.NewLine;
File.WriteAllText(FileLog, Log);
LastActivity = "Writing log file";
Log = Log + Environment.NewLine + "RoboArchiveManager finished at " + DateTime.Now.ToString() + Environment.NewLine + Environment.NewLine;
File.WriteAllText(FileLog, Log);
dancepanda42
dancepanda42•2y ago
YellowDuck
YellowDuck•2y ago
LastActivity = "Writing log file";
Log = Log + Environment.NewLine + "RoboArchiveManager finished at " + DateTime.Now.ToString() + Environment.NewLine + Environment.NewLine;
File.WriteAllText(FileLog, Log);
LastActivity = "Writing log file";
Log = Log + Environment.NewLine + "RoboArchiveManager finished at " + DateTime.Now.ToString() + Environment.NewLine + Environment.NewLine;
File.WriteAllText(FileLog, Log);
dancepanda42
dancepanda42•2y ago
it needs a new line after csharp
YellowDuck
YellowDuck•2y ago
Nice So do you code for a living or for hobbies?
dancepanda42
dancepanda42•2y ago
living but starts with hobby for the Log variable I would recommend a StringBuilder. This is not as computationally intensive as an extension of a string.
YellowDuck
YellowDuck•2y ago
Same. Okej Thanks for the tips. Started working about 1 year ago. But man o man a lot more to learn haha
dancepanda42
dancepanda42•2y ago
or just use the File.AppendAllText method instead of the Log variable
YellowDuck
YellowDuck•2y ago
Okej will try that 🙂 Thank you for taking your time helping me 🙂
'Create objects
sLastActivity = "Create object oFso"
Set oFso = CreateObject("Scripting.FileSystemObject")
'Create objects
sLastActivity = "Create object oFso"
Set oFso = CreateObject("Scripting.FileSystemObject")
This bit is little wired. What will happen if I write
LastActivity = "Create object Fso";

object? Fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
LastActivity = "Create object Fso";

object? Fso = Activator.CreateInstance(Type.GetTypeFromProgID("Scripting.FileSystemObject"));
I do not know that its for
Brady Kelly
Brady Kelly•2y ago
You shouldn't need a FileSystemObject. It looks like you only use it to get a directory, which can be done in a better way You already use Directory.GetFiles to get the files in a directory, what else am I missing the oFso does?
YellowDuck
YellowDuck•2y ago
Yes I do not need LastActivity,
What I was trying to do is Finding folder then files in folder but do not need LastActivity Yes thats true. In the old code oFso is use to GetExtenstionName(FilePath) But I am getting that from Directory
Brady Kelly
Brady Kelly•2y ago
You won't and shouldn't normally need to use reflection, i.e. CreateInstance
YellowDuck
YellowDuck•2y ago
Funny thing is that when I got the assignment I found Telerik code converter so I thought that this will be a easy task haha
Brady Kelly
Brady Kelly•2y ago
Code converters are always dodgy
YellowDuck
YellowDuck•2y ago
Yes they are. Had some help but you two had helped me a lot to day. I appreciate that 🙂 And been writing all day and I am so happy that I order and got my new mouse and keyboard last week
YellowDuck
YellowDuck•2y ago
YellowDuck
YellowDuck•2y ago
My mommy always told me that a good programmer has Logi hahah
Want results from more Discord servers?
Add your server
More Posts