Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

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?
C#CMicrosoft Legacy OS

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon
AndyAinscow

At a guess you do not have the required permissions.
Try running this app as administrator and see what happens in that case.
Peter Chan

ASKER
No, within the machine, I'm able to access the folder, for sure.
AndyAinscow

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Peter Chan

ASKER
I have just checked, now I can access the folder.
sarabande

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
sarabande

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

Sara
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

ASKER
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.
sarabande

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
Peter Chan

ASKER
Many thanks Sara. I've also already got some reasons. Let me further update you. Appreciate you a lot.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
sarabande

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
Peter Chan

ASKER
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?
Peter Chan

ASKER
I'm now only having such problem pending there. Thanks a lot.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

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
Peter Chan

ASKER
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.
ASKER CERTIFIED SOLUTION
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
AndyAinscow

@sara - the foreach is OK, it is clever enough to only call the GetFiles once.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
AndyAinscow

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

thanks Andy.

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


Sara