Jump to content



Photo

vb .net 2012 - Problem with char in folder

Answered Go to the full post

  • Please log in to reply
25 replies to this topic

#1 Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 09 September 2013 - 12:24

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!



Best Answer Nodiaque , 11 September 2013 - 15:01

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!

Go to the full post



#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 09 September 2013 - 19:37

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.



#3 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 09 September 2013 - 19:49

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?



#4 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 09 September 2013 - 20:32

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



#5 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 12:01

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.



#6 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 12:25

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



#7 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 7
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 10 September 2013 - 12:29

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



#8 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 12:29

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.co...m-hamilton.aspx

 

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



#9 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 12:30

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



#10 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 10 September 2013 - 14:38

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...-has-long-paths

 

EDIT: actually I'm not sure that answer is correct. See http://msdn.microsof...y/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.



#11 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 16:28

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



#12 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 16:29

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



#13 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 10 September 2013 - 16:50

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.



#14 OP Nodiaque

Nodiaque

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-December 01

Posted 10 September 2013 - 19:09

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



#15 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 11 September 2013 - 02:07

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?





Click here to login or here to register to remove this ad, it's free!