Directory does exist

Hi,
when running the Console app, having these codes
namespace ProcessSWFile
{
    class Program
    {
        static void Main(string[] args)
        {
            ...
            string dir = args[0];
            try
            {
                foreach (string f in Directory.GetFiles(@dir, "*.out"))
                {
                    ...
            }
            catch
            {
                Console.WriteLine("Directory {0}  \n could not be accessed!", dir);
                return; 
            }      

Open in new window

     

using this
ProcessSWFile f:\dir1

Open in new window


I get
Directory f:\dir1
 could not be accessed!
 

Open in new window

while the folder does exist there. why?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
At a guess you do not have the required permissions.
Try running this app as administrator and see what happens in that case.
HuaMin ChenProblem resolverAuthor Commented:
No, within the machine, I'm able to access the folder, for sure.
AndyAinscowFreelance programmer / ConsultantCommented:
Have you tried or are you assuming?


One sees questions at times that are:
Why doesn't this work, it should work, I do have the required permission.  Eventually the asker of the question does actually test it as administrator AND promptly finds it does work for the admin but not for a 'normal' user, after the permissions are adjusted everything is OK.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

HuaMin ChenProblem resolverAuthor Commented:
I have just checked, now I can access the folder.
sarabandeCommented:
the "could not accessed" message happens in the catch block.

therefore it can be any wrong code within try block that can lead to the message.

you may start your program from visual studio. then the exception thrown should be reported in the output window.

please post full code of the try block.

Sara
sarabandeCommented:
for managed code you may add topic area 'Visual C++.Net' to your questions.

Sara
AndyAinscowFreelance programmer / ConsultantCommented:
>>foreach (string f in Directory.GetFiles(@dir, "*.out"))

Why do you have the '@' symbol before the dir?  Try this:

foreach (string f in Directory.GetFiles(dir, "*.out"))

Open in new window



>>now I can access the folder.
Does that mean it now works when you run it as administrator but not as a normal user?
HuaMin ChenProblem resolverAuthor Commented:
Here are the current codes
https://dl.dropboxusercontent.com/u/40211031/ProcessSWFile.cpp

I do not know why the message does appear, even if the input folder does exist.
sarabandeCommented:
fs = new FileStream(@fld1, FileMode.CreateNew);
here you want to create a new file or directory in a try block without a catch block. if the statement throws an exception it would jump into the catch where the 'could not accessed' was shown. the fld1 is only filled for some conditions which I can't verify that they were true. so in my opinion, the CreateNew will fail (and possibly throw an exception) if you pass fld1 is an empty string or if the fld1 contains the name of an existing directory.

Sara
HuaMin ChenProblem resolverAuthor Commented:
Many thanks Sara. I've also already got some reasons. Let me further update you. Appreciate you a lot.
sarabandeCommented:
the current code is not good. you definitively need to build functions where you now use huge and ugly for loops and long sequences of if blocks such that it is impossible to say whether at end of the sequence you have a good or bad result and which of the many variables (many of them unused!!!) are correctly filled and which are not. each function call either should have a bool or status return which allows you to handle errors in a reasonable way or should be called in a try block where you handle exceptions in a corresponding catch block. especially the code for parsing an input line is so bad that you definitively need to throw it away and make a better approach. we can help you with that if you give an example how the string to parse would look like.

Sara
HuaMin ChenProblem resolverAuthor Commented:
Hi,
I do not know why it cannot further process other files by these codes
            try
            {
                foreach (string f in Directory.GetFiles(dir, "*.out"))
                {
                    ...

Open in new window

as it now only processes 1st file. why?
HuaMin ChenProblem resolverAuthor Commented:
I'm now only having such problem pending there. Thanks a lot.
AndyAinscowFreelance programmer / ConsultantCommented:
replace
catch
            {
                Console.WriteLine("Directory {0}  \n could not be accessed!", dir);
                return; 
            }    

Open in new window


with
           catch (Exception e)
            {
                Console.WriteLine(e.Message);
return;
}

Open in new window


so one actually knows what is going wrong
HuaMin ChenProblem resolverAuthor Commented:
The problem is the process would not pick up other files within the same folder but only process one file. why? There is now no problem when running the application.
sarabandeCommented:
foreach (string f in Directory.GetFiles(@dir, "*.out"))
I am not experienced in c# but I wonder whether the foreach loop can work if you call function GetFiles in the loop condition. I would assume that the function was called again for each loop cycle and you always would retrieve only the first file. if I am right, you should store the collection to a local variable before the foreach:

string[] files = Directory.GetFiles(dir, "*.out");
foreach (string f in files)
{
       ...

Open in new window


Sara

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
@sara - the foreach is OK, it is clever enough to only call the GetFiles once.
AndyAinscowFreelance programmer / ConsultantCommented:
>>The problem is the process would not pick up other files within the same folder but only process one file. why?

I could make a number of wild guesses but - why don't you single step through the code and see what happens yourself.
sarabandeCommented:
thanks Andy.

@Hua
you may nevertheless call the GetFiles before the loop and check how many files were retrieved.


Sara
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.