my network path
//172.12.0.11/karomi snaps/dms/DH2304139/DH2304139_1_2_635023304446654623.jpg
now I tried with below but not worked
string imagePath = "//172.12.0.11/karomi snaps/dms/DH2304139/DH2304139_1_2_635023304446654623.jpg";
imagePath = "#"+"'" + imagePath +"'" ;
The UNC format for a network file path is:
string imagePath = #"\\172.12.0.11\karomi snaps\dms\DH2304139\DH2304139_1_2_635023304446654623.jpg";
I've added an # to avoid the first two \ being escaped. The space itself shouldn't cause an issue, assuming the account the app / site is running under has permission to connect to the share. You shouldn't need to wrap this in single quotes.
You can't do anything without the permission of the application provider, Because they hide the URL of the folder location with some IP or their customized URL.
Related
I have been using a LocalDB.mdf file to build my application, but now need to use it in production and create an installable application.
After research, it seems the best place to store such a file is in the /Appdata/Local/ folder.
I intend to create a new LocalDB.mdf file in there if it doesnt already exist, or has been deleted.
I have a pre-made LocalDB.mdf file in my App Resources, which I wanted to copy into this /Appdata/Local/ folder on first run, but I am seeing an Access is denied error.
I can create a blank file ok to that folder.
Here is the code:
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string dvAppDataFolder = appDataFolder + #"\MyApp";
AppDomain.CurrentDomain.SetData("DataDirectory", dvAppDataFolder);
if (!Directory.Exists(dvAppDataFolder))
Directory.CreateDirectory(dvAppDataFolder);
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
{
File.WriteAllBytes(dvAppDataFolder, LokiServer.Properties.Resources.newDB);
}
In addition, Am I going about this the right way?
This line
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
is probably wrong. Missing the backslash, better to use Path.Combine instead of a string concatenation.
Finally, you want to write to a file not to a folder
string fileName = Path.Combine(dvAppDataFolder,"LocalDB.mdf");
if (!File.Exists(fileName))
{
File.WriteAllBytes(fileName, LokiServer.Properties.Resources.newDB);
}
Are you doing it right? It depends. If your app data should be kept separated for each user of your target machine then you are right, but if you want your database to be available to all users of that machine then use
string appDataFolder = Environment.GetFolderPath
(Environment.SpecialFolder.CommonApplicationData);
I am trying to create directories and upload files to the server, but it won't give me access to. This is my code:
if (FileUploadControl.HasFile)
{
string path = "~/MSImages/";
string mappath = Server.MapPath(path);
if (FileUploadControl.PostedFile.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
string extension = FileUploadControl.PostedFile.FileName;
extension = extension.Substring(extension.LastIndexOf('.'));
if (!Directory.Exists(mappath))
Directory.CreateDirectory(mappath);
string filename = imgext + Request.QueryString["id"];
FileUploadControl.SaveAs(mappath + filename + extension);
}
}
It works when done on my local computer, but not when on my host. How to fix this?
This is most likely a permission issue. Your IIS account user doesn't have the appropriate permissions to change the resource on the other machine.
You could change the IIS user to a low-permission domain user that is just granted access to that specific machine's share. That should fix your issue.
I'm currently playing around with Paths in .Net and have run into some difficultly with regards to replicating a local folder structure passed as a string from a web service to my site running under localhost on IIS Express.
Essentially, our users will select an image within our desktop software, the local path of which will be sent as a property of the image in our web service. So, when my script accesses the web service, it is fed a string such as:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
What our users will then do is FTP this folder structure to a specified directory on our server:
ServerRoot\\umbraco\\axumImages\\Countries\\Canada\\canadianFlag.jpg
The main issue I have here is that I cannot seem to modify the path retreived from the web service to only return the directories from axumImages downwards. So in essence, my local path would be converted to:
axumImages\\Countries\\Canada\\canadianFlag.jpg
I have already tried playing with System.IO.Path to convert this path into the format that I wish to be returned but ultimately all I have acheived so far is either retreiving just the image filename:
canadianFlag.jpg
System.IO.Path.GetFileName(image.FileName);
or the parent directory of the image
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada
Therefore my question is, how can I parse the string so that it is only using axumImages and its descendants?
Any help would be greatly appreciated.
use string.Substring
var startIndex = image.FileName.IndexOf("axumImages");
string test = image.FileName.Substring(startIndex, image.FileName.Length-startIndex)
I ended up solving this in the end using string parsing.
string test = image.FileName.Split(new[] { "axumImages" }, StringSplitOptions.None)[1];
In this example, image.Filename is my filename. So if the filename is:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
this will return
\\Countries\\Canada\\canadianFlag.jpg
I can then concatenate this into a usable variable below which then amounts to the path I require:
var actualPath = "axumImages" + "test";
I am programming in c# and want to copy a folder with subfolders from a flash disk to startup.
Here is my code:
private void copyBat()
{
try
{
string source_dir = "E:\\Debug\\VipBat";
string destination_dir = "C:\\Users\\pc\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
if (!System.IO.Directory.Exists(destination_dir))
{
System.IO.Directory.CreateDirectory(destination_dir);
}
// Create subdirectory structure in destination
foreach (string dir in Directory.GetDirectories(source_dir, "*", System.IO.SearchOption.AllDirectories))
{
Directory.CreateDirectory(destination_dir + dir.Substring(source_dir.Length));
}
foreach (string file_name in Directory.GetFiles(source_dir, "*.*", System.IO.SearchOption.AllDirectories))
{
File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "HATA", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
I got an error:
Could not find a part of the path E:\Debug\VipBat
The path you are trying to access is not present.
string source_dir = "E:\\Debug\\VipBat\\{0}";
I'm sure that this is not the correct path. Debug folder directly in E: drive looks wrong to me. I guess there must be the project name folder directory present.
Second thing; what is {0} in your string. I am sure that it is an argument placeholder because folder name cannot contains {0} such name. So you need to use String.Format() to replace the actual value.
string source_dir = String.Format("E:\\Debug\\VipBat\\{0}",variableName);
But first check the path existence that you are trying to access.
There's something wrong. You have written:
string source_dir = #"E:\\Debug\\VipBat\\{0}";
and the error was
Could not find a part of the path E\Debug\VCCSBat
This is not the same directory.
In your code there's a problem, you have to use:
string source_dir = #"E:\Debug\VipBat"; // remove {0} and the \\ if using #
or
string source_dir = "E:\\Debug\\VipBat"; // remove {0} and the # if using \\
Is the drive E a mapped drive? Then, it can be created by another account other than the user account. This may be the cause of the error.
I had the same error, although in my case the problem was with the formatting of the DESTINATION path. The comments above are correct with respect to debugging the path string formatting, but there seems to be a bug in the File.Copy exception reporting where it still throws back the SOURCE path instead of the DESTINATION path. So don't forget to look here as well.
-TC
Probably unrelated, but consider using Path.Combine instead of destination_dir + dir.Substring(...). From the look of it, your .Substring() will leave a backlash at the beginning, but the helper classes like Path are there for a reason.
There can be one of the two cause for this error:
Path is not correct - but it is less likely as CreateDirectory should create any path unless path itself is not valid, read invalid characters
Account through which your application is running don't have rights to create directory at path location, like if you are trying to create directory on shared drive with not enough privileges etc
File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);
This line has the error because what the code expected is the directory name + file name, not the file name.
This is the correct one
File.Copy(source_dir + file_name, destination_dir + file_name.Substring(source_dir.Length), true);
We just had this error message occur because the full path was greater than 260 characters -- the Windows limit for a path and file name. The error message is misleading in this case, but shortening the path solved it for us, if that's an option.
I resolved a similar issue by simply restarting Visual Studio with admin rights.
The problem was because it couldn't open one project related to Sharepoint without elevated access.
This could also be the issue: Space in the folder name
Example:
Let this be your path:
string source_dir = #"E:\Debug\VipBat";
If you try accessing this location without trying to check if directory exists, and just in case the directory had a space at the end, like :
"VipBat ", instead of just "VipBat" the space at the end will not be visible when you see in the file explorer.
So make sure you got the correct folder name and dont add spaces to folder names. And a best practice is to check if folder exists before you keep the file there.
Regarding the following code sample:
string baseLocation = HttpContext.Current.Server.MapPath("/");
const string templateName = #"//temp//ExportTemplate.xlsx";
const string generatedLocation = #"{0}//temp//{1}";
var fileName = string.Format("Export-{0}.xlsx", DateTime.Now.Date.ToString("yyyy-MM-dd"));
var newFile = String.Format(generatedLocation, baseLocation, fileName);
File.Copy(baseLocation + templateName, newFile, true);
We are using this on a production server and a local dev environment (via a site in IIS). Both are running IIS 7.5. The code works correctly on production, but throws an error in local dev:
Access to the path 'C:\Path\To\Site\//temp//Export-2013-01-29.xlsx' is denied.
The file is created/copied correctly on local dev, but I'm guessing it's erroring out due to the slashes in the path being incorrect. The app pool identity has full access to the 'temp' folder.
This brings up a couple questions:
In this situation, what does the '//' do to path? I understand '\' is the way to escape a backslash, but the '//' doesn't make sense.
Could there be a difference in the configuration of the two environments that makes the generated path work correctly on the production server but fail in my local dev?
The code should be using a \, not / for file paths.
Either
const string templateName = #"\temp\ExportTemplate.xlsx";
or
const string templateName = "\\temp\\ExportTemplate.xlsx";
would work fine. It's surprising the current version of the code works in production,it may be due to that windows is built to allow either forward or back slashes in file paths. (this goes back to DOS days when many of the users were also UNIX users)
In addition, I'd recommend using Path.Combine rather than just concatenating the strings for the file path (this will get help avoid getting extra slashes or forward slashes in paths like "C:\Path\To\Site\\temp\Export-2013-01-29.xlsx"). ex:
File.Copy(Path.Combine(baseLocation, templateName), newFile, true);
// will always give you // ... With the # at the string, it is a verbatim string literal and you do not need to escape characters. Therefore you can use \ to get . If you drop the #, you will need to use \ to get . When you are working with file paths, it is always a backslash(). When working with URL paths, it is always a forward slash(/)