• 0

vb .net 2012 - Problem with char in folder


Question

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!

Link to comment
Share on other sites

25 answers to this question

Recommended Posts

  • 0

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.

Link to comment
Share on other sites

  • 0

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?

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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)?

Link to comment
Share on other sites

  • 0

Seems like I can't edit my post.

 

As for the unicode part, it's from a post from MSDN that was saying that using \\?\ to call the unicode version of the api

 

http://blogs.msdn.com/b/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx

 

But, it seems I'm doing something wrong because when I happen that to my path, it says path invalid.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

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?

Link to comment
Share on other sites

  • 0

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.

Link to comment
Share on other sites

  • 0

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:

Link to comment
Share on other sites

  • 0

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/

  • Like 1
Link to comment
Share on other sites

  • 0

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!).

Link to comment
Share on other sites

  • 0

(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?

Link to comment
Share on other sites

  • 0

(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.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.