How to get (by code) the "Sources" folder, or work with relative paths ?

Created at 05 Feb 2021, 20:33
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
JE

JerryTrader

Joined 06.01.2021

How to get (by code) the "Sources" folder, or work with relative paths ?
05 Feb 2021, 20:33


Hi guys, 

Given this parameter:

[Parameter("Sound Path")]
public string InSound { get; set; }
  1. How can I get, by code, the "Sources" folder, or the cBot folder, or anything else, so that the user don't have to type the full path of a file, but just a relative path ?
  2. If it is not possible, does the Notifications.PlaySound work with any relative path ? Is so, what is this folder ?

 

Thanks for your answer !
Cheers,
Jerry


@JerryTrader
Replies

firemyst
06 Feb 2021, 13:09

You have a few possible options:

 

1) Use the built in system sounds, and then configure what the sounds will be in the Windows Control panel:

System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
System.Media.SystemSounds.Exclamation.Play();

 

2) Get the current executing directory and use that as appropriate:

 

3) If you did a simple Google search, you would have found that the sound files for Windows are kept in C:\Windows\Media folder. Put all sound files in there, and prepend the path to whatever file name the user selects.

 

Hope that helps!

 


@firemyst

JerryTrader
06 Feb 2021, 14:33

Thanks firemyst !

 

1) Use the built in system sounds, and then configure what the sounds will be in the Windows Control panel:

Good to know, I wasn't aware of such solution. Nevertheless, this will change the whole system sounds, and is not specific to an indicator or a cBot.

 

2) Get the current executing directory and use that as appropriate:

I already tried all those ways, but nothing is pointing to where the *.algo file is stored, but rather to where cTrader is executed from, which is a bit random and unpredictable since cTrader uses ClickOnce.

 

3) If you did a simple Google search, you would have found that the sound files for Windows are kept in C:\Windows\Media folder. Put all sound files in there, and prepend the path to whatever file name the user selects.

I did, and I currently use this for now, but I would like to deliver something where the user just has to copy/paste the *.algo file + the sound folder and that's it.

Since cTrader knows where to look for *.algo files, maybe they expose this path somewhere in the API.

 

@cTrader :
If it is not exposed, would it be possible to add this to the API ? Just a property on cAlgo.API.Internals.Algo, like AlgoPath, or something like that ?

Meanwhile, does cTrader only look into Documents\cAlgo\Sources\, or, depending on the setup, it could look into other folders ?


@JerryTrader

firemyst
06 Feb 2021, 16:02

I think you might be over-engineering things.

All cTrader bots are installed and run from:

C:\Users\<user name>\Documents\cAlgo\Sources\Robots

just as all indicators are installed and run from:

C:\Users\<user name>\Documents\cAlgo\Sources\Indicators

So why not just prepend that path in the bot code?

Or within each folder make one called "sounds", stick the sounds in there, and prepend the path

C:\Users\<user name>\Documents\cAlgo\Sources\Robots\sounds

??

 

If the users writing the code don't know the path, then provide them with a dll that has a static constant string variable with the path? They'll have to include that dll in their projects, but so what?

 


@firemyst

JerryTrader
06 Feb 2021, 16:23

RE:

Hence my last question: "does cTrader only look into Documents\cAlgo\Sources\, or, depending on the setup, it could look into other folders ?"
In the long term, I don't think it is over-engineering to rely on an API property rather than guessing the path will always be the "Documents" folder.

For example, on my laptop, the Robots folder is on the D drive, so hardcoding a "C:\Users\<user name>\Documents\cAlgo\Sources\Robots" would not work.

So I guess that it always look into the Windows "Documents" folder (I can deal with it if there is no exposed property), but it would be nice to have a confirmation because I can't find it anywhere on the documentation.


@JerryTrader

firemyst
07 Feb 2021, 04:27

RE: RE:

JerryTrader said:

Hence my last question: "does cTrader only look into Documents\cAlgo\Sources\, or, depending on the setup, it could look into other folders ?"
In the long term, I don't think it is over-engineering to rely on an API property rather than guessing the path will always be the "Documents" folder.

For example, on my laptop, the Robots folder is on the D drive, so hardcoding a "C:\Users\<user name>\Documents\cAlgo\Sources\Robots" would not work.

So I guess that it always look into the Windows "Documents" folder (I can deal with it if there is no exposed property), but it would be nice to have a confirmation because I can't find it anywhere on the documentation.

It can look into other folders. For instance, through bots I create subfolders within the c:\windows\temp folder (which we know is always there because it's part of Windows) to store data, sounds, logs, and other information.

However, depending on the security permissions, you may have to grant your bots/indicators "full permissions" (or something else) other than the default of "none".

 

If you do go that route, make sure you have some sort of "clean up" happening too. Otherwise your files/folder will accumulate and slowly suck up your drive's free space.


@firemyst