Sign in to follow this  
Followers 0
Nodiaque

vb .net 2012 - Problem with char in folder

26 posts in this topic

Hello,

 

I have a little issue with my code. In fact, I have two.

 

What I want to do is delete a folder with all subfolders and files. I tried 2 ways:

 

The first one:

Dim di as New DirectoryInfo(spath)
For Each diChild as DirectoryInfo In di.getdirectories()
TraverseDirectory(diChild)
Next

CleanAllFilesInDirectory(di)

di.delete()

The other one:

My.Computer.FileSystem.DeleteDirectory(sPath,FileIo.DeleteDirectoryOption.DeleteAllContents)

Both of the method work, except for 2 occurence:

 

1) When a folder contain a special character (not illegal), like ?, it throw an exception (it says access denied but that is clearly not the problem).

2) When a folder is longer then MAX_PATH

 

For the second problem, I read at some place on the web to use unicode path, but I find it only for c++ and when I try to convert to vb.net, I get errors.

 

Does anyone have a clue on how to solve this two problem?

 

Thanks!

Share this post


Link to post
Share on other sites

My.Computer is a VB-specific namespace with which I'm not familiar. I believe it's just a proxy for a bunch of different classes in .NET. The real .NET class you want to use is System.IO.Directory, see this example. I don't have high hopes that this will fix the issue you're having, but at least you're more likely to find help by using the real class and method names than the VB aliases.

 

.NET strings are Unicode strings, unlike C++ std::string or std::wstring or const char* or the zillion other string types there are in there, just so you know.

Share this post


Link to post
Share on other sites

Looks like the path length issue is a real, hard limitation in .NET. You just can't deal with that and stay inside .NET. Might be able to get around .NET through P/Invoke but I don't know what Win32 function you'd want.

 

Special character issue seems baffling, .NET strings are native Unicode, should handle that without issue. Where do you get your path name from?

Share this post


Link to post
Share on other sites

I've hit that limit before, when installing extensions.  Brainless idiots setup their paths using silly nested company names and personal names and project names and so on, and clearly never actually tested the installers, which obviously crash. :p

Share this post


Link to post
Share on other sites

My.Computer is a VB-specific namespace with which I'm not familiar. I believe it's just a proxy for a bunch of different classes in .NET. The real .NET class you want to use is System.IO.Directory, see this example. I don't have high hopes that this will fix the issue you're having, but at least you're more likely to find help by using the real class and method names than the VB aliases.

 

.NET strings are Unicode strings, unlike C++ std::string or std::wstring or const char* or the zillion other string types there are in there, just so you know.

Yeah I've tried System.IO too (wasn't remembering the exact command), which is seemlessly the same has my.computer (My namespace was introduced in 2008 to simply operation for new programmer, I tried it even if I didn't had high hope). Good to know it's unicode in .net, just that I was reading to use @\\ as prefix to my path, which would transform in unicode thus going over the MAX_PATH lenght, but this might be c++ only.

 

Looks like the path length issue is a real, hard limitation in .NET. You just can't deal with that and stay inside .NET. Might be able to get around .NET through P/Invoke but I don't know what Win32 function you'd want.

 

Special character issue seems baffling, .NET strings are native Unicode, should handle that without issue. Where do you get your path name from?

I get the filename from a listing command. From AD, I get the user profile folder path, then I invoke the delete path from there. Thing is, in the path, the person created folder name with ?, which return a "access is denied" error. But the access isn't a problem since I'm using AD authentication from Windows Session, and from Windows, I can delete the folder no problem.

 

I've hit that limit before, when installing extensions.  Brainless idiots setup their paths using silly nested company names and personal names and project names and so on, and clearly never actually tested the installers, which obviously crash. :p

Yeah, working in a school, I got students having fun doing this specially to annoy us. I've found various software online that are able to delete them, but I'd like to be able to delete them with my own software, which already have a lot of stuff coded in it, but is in VB .net.

Share this post


Link to post
Share on other sites

Ok, I just tried something and it seems that the ? isn't the problem for the access denied error. I've renamed every bad folder to remove any ?, leaving only - or _ and I still get "access to the path <path> is denied", though I have the authorisation to delete this folder... This is using My.Computer... I'll try with System.IO

Share this post


Link to post
Share on other sites

Is it possible that Access Denied is because the folder is in use by another application (e.g. do you have a command prompt open in that folder)?

Share this post


Link to post
Share on other sites

Is it possible that Access Denied is because the folder is in use by another application (e.g. do you have a command prompt open in that folder)?

 

Nope, I even was able to use it just to go browsing in it and hit delete... weird

Share this post


Link to post
Share on other sites

Does running your application as administrator fix the Access Denied? 

 

For long paths, as I was saying you'll have to P/Invoke your way around .NET to get the Unicode version of DeleteFile. Then prefix your path with "\\?\". Here's how it's done in C#, not familiar with the VB.NET syntax:

using System;  
using System.Runtime.InteropServices;  
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]  
[return: MarshalAs(UnmanagedType.Bool)]  
internal static extern bool DeleteFile(string lpFileName);

See http://stackoverflow.com/questions/2223007/c-sharp-deleting-a-folder-that-has-long-paths

 

EDIT: actually I'm not sure that answer is correct. See http://msdn.microsoft.com/en-us/library/aa363872.aspx :
 

To delete an existing directory, use the RemoveDirectory or RemoveDirectoryTransacted function. Before removing a directory, you must ensure that the directory is empty and that you have the delete access privilege for the directory. To do the latter, call the GetSecurityInfo function.

Looks like you may have to delete all files in the directory recursively prior to deleting it. See the documentation for DeleteFile on how to do this.

Share this post


Link to post
Share on other sites

UAC is disabled and is running under my creditential, which is domain admin, and I'm running it from vstudio. When right-click on my apps, I don't have the run as administrator option, since it's using windows session for all securities

Share this post


Link to post
Share on other sites

I'll try to see about the code from c#. I've seen it in my other link, just wasn't sure how to convert it to vb .net

Share this post


Link to post
Share on other sites

I'll try to see about the code from c#. I've seen it in my other link, just wasn't sure how to convert it to vb .net

 

A little suggestion.  Drop VB.Net and go with C#.  There are way more C# jobs out there and the skills are more easily transferable to things like Java.

Share this post


Link to post
Share on other sites

Yeah I know about C#, but I've been coding in vb .net for the last 10 years and working in a school that teach Java and VB .Net, I don't to about C# pretty much. I know Java, my school formation was in Java (but in 2003, never used since). As for the job, I'm aware of it, I'm currently at a university that still teach in C++ (the last one in quebec, they all moved to Java) and I'm lost.

 

Thanks

Share this post


Link to post
Share on other sites

Yeah I know about C#, but I've been coding in vb .net for the last 10 years and working in a school that teach Java and VB .Net, I don't to about C# pretty much. I know Java, my school formation was in Java (but in 2003, never used since). As for the job, I'm aware of it, I'm currently at a university that still teach in C++ (the last one in quebec, they all moved to Java) and I'm lost.

 

Thanks

Polytechnique?

Share this post


Link to post
Share on other sites

Nope, Universit? Laval. They have some classes in Java, but most of them are in C++. Polytechnique is in Java, at least that's what the rep told me.

Share this post


Link to post
Share on other sites

Ok, I've found a quick way to solve all my problems: AlphaFS library. It's a rewrite of all of the System.IO with support for long path (\\?\). I've imported that and voila!

1 person likes this

Share this post


Link to post
Share on other sites

Glad you've found an answer. However, when you have more time, do come back to the problem and find a more native solution. :)  It's all well and good using external frameworks, but they're "black boxes" and it's not good to rely on other people's black boxes as you don't really know what they're doing.

Share this post


Link to post
Share on other sites

Nope, Universit? Laval. They have some classes in Java, but most of them are in C++. Polytechnique is in Java, at least that's what the rep told me.

Nah it's C++, I just finished my degree there and the intro course to OOP still is C++. Only Java we did was in Data Structures and Algorithms IIRC, the rest was C++/C#.

 

Perhaps the rep was trying not to scare you  :laugh:

Share this post


Link to post
Share on other sites

Glad you've found an answer. However, when you have more time, do come back to the problem and find a more native solution. :)  It's all well and good using external frameworks, but they're "black boxes" and it's not good to rely on other people's black boxes as you don't really know what they're doing.

 

Well, it is a native solution. The library is a .net library that have rewritted the System.IO to use pinvoke kernel32 to allow \\?\ path (thus unicode long path). Simply, like I always been told, why reinvent the wheel when it's already invented? Instead of myself adding a lot of code to invoke kernel32 (which is a pain in vb .net), I included a library that already did that. As for the "you don't really know what they're doing", the code is open source on that dll, and everything is served with a well made doc.

 

http://alphafs.codeplex.com/

1 person likes this

Share this post


Link to post
Share on other sites

I didn't mean to imply that they were up to no good, just that it's good to know the actual solution without having to rely on other people's code solely.  One never knows when one is in a situation where it's not possible, or allowed, to use third party code or libraries (something I have to put up with!).

Share this post


Link to post
Share on other sites

(there's no way to edit post on neowin? I'm sure there was a way before)

 

Nah it's C++, I just finished my degree there and the intro course to OOP still is C++. Only Java we did was in Data Structures and Algorithms IIRC, the rest was C++/C#.

 

Perhaps the rep was trying not to scare you  :laugh:

 

Weird, but isn't Polytechnic for ingeneering?

Share this post


Link to post
Share on other sites

(there's no way to edit post on neowin? I'm sure there was a way before)

 

 

Weird, but isn't Polytechnic for ingeneering?

Yeah, well there's software engineering and "computer engineering" (g?nie informatique) which are basically equivalent to computer science except you get a cheap steel ring and you can sign .Ing after.

Share this post


Link to post
Share on other sites

Hahaha, yeah, what we call "gningningnieur" ;D

Share this post


Link to post
Share on other sites

Hahaha, yeah, what we call "gningningnieur" ;D

I'm fine with others' inferiority complexes ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.