Moving VB's FileSystemObject to C#
-
I am in the process of moving VB code to C#. However, I have encountered a stumbling block. I have the following code in VB below that uses the Scripting.FileSystemObject and the Scripting.File object. How would I program this property below in C#? Would I use the System.IO.File in place of the Scripting.File object? If so, what would I use for the Scripting.FileSystemObject? Would I open a filestream? If I do, FileSystemObject grabs the whole file, how do I do that using filestreams? VB Code... Private mvarFSO As Scripting.FileSystemObject Private mvarLocalFile As Scripting.File Public Property Get LocalFile() As Scripting.File Set mvarLocalFile = Nothing If mvarFSO.FileExists(mvarMyVSSItem.LocalSpec) Then Set mvarLocalFile = mvarFSO.GetFile(mvarMyVSSItem.LocalSpec) End If Set LocalFile = mvarLocalFile End Property C# Translation... private something_like_FileSystemObject mvarFSO; private something_like_File_object mvarLocalFile; public return_type LocalFile { get { ??? return mvarLocalFile; } } Help please, I'm stumped... Eric
-
I am in the process of moving VB code to C#. However, I have encountered a stumbling block. I have the following code in VB below that uses the Scripting.FileSystemObject and the Scripting.File object. How would I program this property below in C#? Would I use the System.IO.File in place of the Scripting.File object? If so, what would I use for the Scripting.FileSystemObject? Would I open a filestream? If I do, FileSystemObject grabs the whole file, how do I do that using filestreams? VB Code... Private mvarFSO As Scripting.FileSystemObject Private mvarLocalFile As Scripting.File Public Property Get LocalFile() As Scripting.File Set mvarLocalFile = Nothing If mvarFSO.FileExists(mvarMyVSSItem.LocalSpec) Then Set mvarLocalFile = mvarFSO.GetFile(mvarMyVSSItem.LocalSpec) End If Set LocalFile = mvarLocalFile End Property C# Translation... private something_like_FileSystemObject mvarFSO; private something_like_File_object mvarLocalFile; public return_type LocalFile { get { ??? return mvarLocalFile; } } Help please, I'm stumped... Eric
Not sure how much help this will be but... You can still use the Scripting.FileSystemObject in C# if you would like. The code would be pretty close to what you have already, just change it to use the new syntax. Very quick and easy. You can also use the IO libraries provided by .NET but you will, obviously, have to re-do all the code that calls this property. Off the top of my head, I can't really tell you what needs to change but I would probably stick with the Scripting.FileSystemObject if you're porting. It's usually easier to change as little as possible and make sure it's working before moving on. Cheers, steve
-
I am in the process of moving VB code to C#. However, I have encountered a stumbling block. I have the following code in VB below that uses the Scripting.FileSystemObject and the Scripting.File object. How would I program this property below in C#? Would I use the System.IO.File in place of the Scripting.File object? If so, what would I use for the Scripting.FileSystemObject? Would I open a filestream? If I do, FileSystemObject grabs the whole file, how do I do that using filestreams? VB Code... Private mvarFSO As Scripting.FileSystemObject Private mvarLocalFile As Scripting.File Public Property Get LocalFile() As Scripting.File Set mvarLocalFile = Nothing If mvarFSO.FileExists(mvarMyVSSItem.LocalSpec) Then Set mvarLocalFile = mvarFSO.GetFile(mvarMyVSSItem.LocalSpec) End If Set LocalFile = mvarLocalFile End Property C# Translation... private something_like_FileSystemObject mvarFSO; private something_like_File_object mvarLocalFile; public return_type LocalFile { get { ??? return mvarLocalFile; } } Help please, I'm stumped... Eric
Hello, the codegurus around the world.;) Just hints.
try
{
string tempS = "You need the right path";
FileSystemObject fso = new FileSystemObject();
Scripting.File fInfo = fso.GetFile(tempS);
}
catch {fInfo = null;}Please, don't send me your email about your questions directly. Have a nice day! Sonork - 100.10571:vcdeveloper ;)
-Masaaki Onishi-
-
I am in the process of moving VB code to C#. However, I have encountered a stumbling block. I have the following code in VB below that uses the Scripting.FileSystemObject and the Scripting.File object. How would I program this property below in C#? Would I use the System.IO.File in place of the Scripting.File object? If so, what would I use for the Scripting.FileSystemObject? Would I open a filestream? If I do, FileSystemObject grabs the whole file, how do I do that using filestreams? VB Code... Private mvarFSO As Scripting.FileSystemObject Private mvarLocalFile As Scripting.File Public Property Get LocalFile() As Scripting.File Set mvarLocalFile = Nothing If mvarFSO.FileExists(mvarMyVSSItem.LocalSpec) Then Set mvarLocalFile = mvarFSO.GetFile(mvarMyVSSItem.LocalSpec) End If Set LocalFile = mvarLocalFile End Property C# Translation... private something_like_FileSystemObject mvarFSO; private something_like_File_object mvarLocalFile; public return_type LocalFile { get { ??? return mvarLocalFile; } } Help please, I'm stumped... Eric
using System.IO;
...
private FileInfo mvarLocalFile;public FileInfo LocalFile
{
get
{
string path = mvarMyVSSItem.LocalSpec;
if (File.Exists(path))
mvarLocalFile = new FileInfo(path);
else
mvarLocalFile = null;return mvarLocalFile; }
}
-
I am in the process of moving VB code to C#. However, I have encountered a stumbling block. I have the following code in VB below that uses the Scripting.FileSystemObject and the Scripting.File object. How would I program this property below in C#? Would I use the System.IO.File in place of the Scripting.File object? If so, what would I use for the Scripting.FileSystemObject? Would I open a filestream? If I do, FileSystemObject grabs the whole file, how do I do that using filestreams? VB Code... Private mvarFSO As Scripting.FileSystemObject Private mvarLocalFile As Scripting.File Public Property Get LocalFile() As Scripting.File Set mvarLocalFile = Nothing If mvarFSO.FileExists(mvarMyVSSItem.LocalSpec) Then Set mvarLocalFile = mvarFSO.GetFile(mvarMyVSSItem.LocalSpec) End If Set LocalFile = mvarLocalFile End Property C# Translation... private something_like_FileSystemObject mvarFSO; private something_like_File_object mvarLocalFile; public return_type LocalFile { get { ??? return mvarLocalFile; } } Help please, I'm stumped... Eric
I would use the System.IO namespace... To me it's more of a cleaner interface than the plain old FileSystemObject. Though I'm partial to C#. :) System.IO.FileInfo fileinfo = new System.IO.FileInfo(filename); fileinfo.Open(System.IO.FileMode.OpenOrCreate);
-
I would use the System.IO namespace... To me it's more of a cleaner interface than the plain old FileSystemObject. Though I'm partial to C#. :) System.IO.FileInfo fileinfo = new System.IO.FileInfo(filename); fileinfo.Open(System.IO.FileMode.OpenOrCreate);