Reputation: 393
I tried to make this function (and others) and put it in a separate class file in my project that's under "/Helpers/UploadFiles.cs"
namespace Artikelhantering.Helpers
{
public class UploadFiles
{
private void EnsureDirectoriesExist(string SKU)
{
// if the directory doesn't exist - create it.
if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
{
System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
}
}
}
Then in the controller I added using Artikelhantering.Helpers;, it's also added to the namespace section of the web.config file and also to global.asa.cx.
Then I figured I could call it from an ActionResult in my controller like this
[ChildActionOnly]
public ActionResult _EnumerateFolder(string SKU)
{
// create directory if it does not exist
EnsureDirectoriesExist(SKU);
ViewBag.SKU = SKU;
var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
return View(folder);
}
But all I get is:
Error 2 The name 'EnsureDirectoriesExist' does not exist in the current context
Tried calling it by writing it as UploadFiles.EnsureDirectoriesExist(); but that doesn't work either. How am I supposed to call these methods without having them all in the same file? I would like to organize this better.
Upvotes: 0
Views: 41914
Reputation: 6260
So:
public static class Helper
{
public static void EnsureDirectoriesExist(string SKU)
{
...
}
}
Upvotes: 1
Reputation: 1640
First, change the access modifier of EnsureDirectoriesExist
to public then
try to change your ActionResult _EnumerateFolder
method with the code below:
public ActionResult _EnumerateFolder(string SKU)
{
// create directory if it does not exist
new UploadFiles.EnsureDirectoriesExist(SKU);
ViewBag.SKU = SKU;
var folder = Directory.EnumerateFiles("//servername/wwwroot/prodimg/" + SKU);
return View(folder);
}
Upvotes: 1
Reputation: 380
make your directory method public and static. Then you can call it something like this
Artikelhantering.Helpers::UploadFiles.EnsureDirectoriesExist(SKU);
If you can't change the signature... you can make a public wrapper method and call it the same way. If you cannot make the method static, then you should create first an instance of your class and finally call the new public wrapper method.
Upvotes: 0
Reputation: 32481
Mark your class as static then try this:
public static class UploadFiles
{
public void EnsureDirectoriesExist(string SKU)
{
//your code
}
}
Then:
public ActionResult _EnumerateFolder(string SKU)
{
UploadFiles.EnsureDirectoriesExist(SKU);
//your code
}
Upvotes: 0
Reputation: 56697
The method is private. You can not access private members of other classes.
Also some other problems here:
UploadFiles.EnsureDirectoryExists()
, you need to make it a class method (static
).Sample code for 1):
UploadFiles uf = new UploadFiles();
uf.EnsureDirectoryExists();
Sample code for 2):
public class UploadFiles
{
public static void EnsureDirectoriesExist(string SKU)
{
// if the directory doesn't exist - create it.
if (!System.IO.Directory.Exists("//servername/wwwroot/prodimg/" + SKU))
{
System.IO.Directory.CreateDirectory("//servername/wwwroot/prodimg/" + SKU);
}
}
}
I furthermore suggest that you google for a C# tutorial that provides you with information on what classes are and how they can be used.
Upvotes: 5