Change directory

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1043 Views - Last Post: 19 November 2019 - 01:32 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Change directory

Posted 07 November 2019 - 12:51 PM

My new job is asking me to learn C# as well as work as a python programmer. I am in the middle of writing a program to automate the maintenance of the computers at work. I know I could make a command in the windows to do it, but I would like to try and figure out how to do it myself.

I know in C++ to change directories I could do something like this:
    std::cout << "Which directory do you want to enter?:" << std::endl;
    std::string change;
    std::cin >> change;
    _chdir(change.c_str());
    system("dir");


I tried troubleshooting it as well as looking up in google and I can't find a way to have the user select which directory they can enter. If someone would please give me a hint or a suggestion I would really appreciate it

Dreamincode wont let me show my code, every time I try it blocks me saying I am a hacker.

This post has been edited by albert003: 07 November 2019 - 12:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change directory

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Change directory

Posted 07 November 2019 - 12:56 PM

Is this a winform, wpf, console app, or what?
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Change directory

Posted 07 November 2019 - 01:00 PM

console app

Would you mind terribly if I tried to message you my program? Maybe it will let me do that without blocking me.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Change directory

Posted 07 November 2019 - 01:03 PM

A directory is just a string path. There's nothing to select in a console app unless you spit out some numbered options and have the user pick from those.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Change directory

Posted 07 November 2019 - 01:14 PM

Example of what I mean:

Current Directory: C:\test
Available:
0 : C:\test\abc
1 : C:\test\def
2 : C:\test\old_river
3 : C:\test\up
Select one to open:
2
Selected: 2 : C:\test\old_river
-----------------
Current Directory: C:\test\old_river
Available:
No directories

Go back up a directory?



That's all just strings, a few loops, and string arrays utilizing the 'Directory.GetDirectories' functionality.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Change directory

Posted 07 November 2019 - 06:30 PM

This:
std::string change;
std::cin >> change;


Is simply:
string change = Console.ReadLine();


You should recall this from your Popmachine thread.

_chdir() is simply a matter of setting the Enviroment.CurrentDirectory.
Was This Post Helpful? 0
  • +
  • -

#7 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Change directory

Posted 07 November 2019 - 10:20 PM

That was such a simple solution I feel like an idiot for not thinking about it earlier. Thank you everyone, I will tried to post my solution here but the website keeps blocking me thinking I am trying to hack the system.

What I am trying to do is have the program go to cmd, let the user select which directory and then it will run chkdisk and defragmentor.

skydiver
I review the pop machine program and I couldn't find the reference you mentioned. Would you please be so kind to tell me which post it was in?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Change directory

Posted 07 November 2019 - 10:48 PM

Post #9. Lines 27 and 95.
Was This Post Helpful? 0
  • +
  • -

#9 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Change directory

Posted 11 November 2019 - 03:39 PM

SkyDiver
I didn't realise I could use that in command using C#.

modi123_1
That actually makes my life a lot easier.

This is the problem I have with the program. I need to run cmd as an administrator so I can use chk dsk and disk defragmantor. I figured out a way to do that but I cannot change directories to the path the user wanted. I troubleshooted my program and went through a few tutorials on my own and I was able to change directories without running cmd as an administrator. I've tried everything I could thing of to show my work and I keep getting blocked. I put it on githiub this is the link to the program. I put only the function Maintenance.


https://github.com/a...03?tab=projects
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Change directory

Posted 11 November 2019 - 03:50 PM

The site is correct. You cannot upload an .EXE here.

https://github.com/a...03/copy-program
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Change directory

Posted 11 November 2019 - 08:52 PM

I feel that you are failing to tell us one your requirements or something that you are doing. Defrag and Chkdsk are volume level tools. It doesn't matter what directory you are in top use them.

Also, what is the folder you are trying to change to? How are you observing that the program failed to change directors when as admin, and succeeds when a normal user?
Was This Post Helpful? 0
  • +
  • -

#12 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Change directory

Posted 12 November 2019 - 06:28 PM

modi123_1

I tried to copy and paste my code as well as putting my code on github. Both ideas I was blocked from posting my code here.

SkyDiver

My Idea is to make the C# program let the user choose which directory they wanted to enter and have it run chkdsk and disk defrag.

Since the site won't let me post my code my idea is something like this:

"Which directory do you want to enter?:"
string path;
path = Connvert.ToInt32(Console.ReadLine());

cmd runs as administrator
changes to the directory the user wanted.
program runs chk dsk and disk defrag

I figured out how to run the program in cmd and change directory. But when I run it as an administrator, I can't figure out how to do it. I know I can make Windows do it, but I wanted to be proactive and have the user do it when they wanted.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Change directory

Posted 12 November 2019 - 08:57 PM

Let's step back: Why do you need to change directories? It doesn't matter if I run chkdsk or defrag from "C:\" or from "C:\Users\Robert\Downloads". Both programs will still ask me which drive needs to be checked or defragmented.

For git, most people just upload source code, not binaries. If you are trying to upload binaries you are likely doing something wrong with source control.

As for your code above. Stop and think about it:
string path;
path = Connvert.ToInt32(Console.ReadLine());


Why are you trying to get a string from the user using ReadLine(), then converting it to an Int32, then try to assign it to the the variable path which is a string. Shouldn't you just assign the the string directly?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Change directory

Posted 13 November 2019 - 08:29 AM

Anyway, I don't seem to have any problems changing directories while as a regular user or as an admin:
Posted Image

Spoiler


Are you sure that you don't have some kind of AntiVirus software, or nanny-ware that is blocking your application from doing certain things while running as administrator?
Was This Post Helpful? 0
  • +
  • -

#15 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Change directory

Posted 13 November 2019 - 09:48 PM

Skydiver
I know I could do it in command and its not hard to do. I wanted to make it user friendly as an easy program to make for my new job and use C#.

I have Norton anti virus on my laptop and I don't have any nanny ware installed either. I'll try using my desktop to post my program and see if it blocks me again.

I have a few new ideas how to make the program work, I'll give them a try and if by Monday I cannot figure it out; I'll take a peek at your spoiler.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2