i know im doing something small wrong im just braindead right now. The path is the problem, i tried to use path.combine and also put system.IO in front of it but nothing worked.
string path = (Environment.SpecialFolder.ApplicationData + #"\MSPhone").ToString();
File.SetAttributes(path, FileAttributes.Hidden);
you need to use Environment.GetFolderPath along with your SpecialFolder. Environment.SpecialFolder.ApplicationData is just an enum value, not representative of the path without acting on it:
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"MSPhone");
this gives me "C:\Users\jonesopolis\AppData\Roaming\MSPhone"
and of course, always use Path.Combine rather than building the path yourself.
Related
I'm building a webapp using ASP.NET.
On my physical hard drive:
The path for my text file is: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Data\TextFiles\someFile.txt
The .cs file is located in: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\someCSFile.cs
In my code, I have the followings:
string fileName= Server.MapPath("TextFile/someFile.txt");
The code throws an exception saying that Could not find a part of the path 'D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\TextFile\someCSFile.cs
How am I going to use Server.MapPath to make it "go up one level", then find the "Data" folder > "TextFiles" > finally the "someFile.txt" WITHOUT hardcoding the entire file path?
This should do the trick
string fileName= Server.MapPath(#"..\Data\TextFile\someFile.txt");
take a look at this:
StackOverflow Post about Server.MapPath
You can use .. to go up one level:
string fileName= Server.MapPath("../Data/TextFile/someFile.txt");
You can also start from the application root by starting the path with a slash:
string fileName= Server.MapPath("/Data/TextFile/someFile.txt");
I want to copy a file to a directory. I thought it would be a simple enough process.
This is the code im using:
string strSrcPath = "C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = "C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, sourceFile), strDstPath);
The problem here is that when i'm doing the File.Copy it wants to copy one file to another, but I dont want to do that since the file does not exist in the destination path. Therefore I get thrown an error which states something along the lines of 'Cannot copy, strDstPath is a destination not a file"
Was there something I could use instead of File.Copy to copy a file that doesnt exist in the destinaion from the source to destination?
The problem is that the parameters are the source filename and the destination filename. You are passing a destination directory and the program is confused because you can't make the file into a directory.
Use instead:
File.Copy(Path.Combine(strSrcPath , strFile ), Path.Combine(strDstPath, strFile);
You seem to be passing some wrong parameter to the Path.Combine (the second one). It should be strFile instead of sourceFile which is quite unclear where is it coming from.
And you also need to provide a filename for the destination folder:
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
You also need to escape the \ characters in your string because your code will probably not compile. This could be done by either using \\ or by using the # character at the beginning of your string.
string strSrcPath = #"C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = #"C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
Also make sure that the destination folder you specified exists. If it doesn't exist you need to create it first (using the Directory.CreateDirectory method).
You have to specify a filename for your destination
so
File.Copy("XMLFile1.xml", #"c:\temp");
will fail where
File.Copy("XMLFile1.xml", #"c:\temp\XMLFile1.xml");
will not
I've checked this thread How to get files in a relative path in C#, the directory is in IDE, which is not correct for me. I have a website application, needs to get image files from the same level folder img. I can use following code to get them:
DirectoryInfo path=new DirectoryInfo(#"c:\WebsiteSolution\wwwroot\Chat\img");
FileInfo[] images = path.GetFiles("*");
But I want to use something like .\img to replace the parameter in the first line code, is that possible?
Call the Server.MapPath utility to get the relative path.
DirectoryInfo path = Server.MapPath("~\Chat\img");
For ASP.Net use MapPath - http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx.
Also if you just need to construct path for rendering a page just /Chat/img/My.gif is enough.
You need to find an anchor - something like Application.ExecutablePath - and then use that anchor with Path.Combine() to reach your image directory.
If your Application.ExecutablePath were: #"c:\WebSiteSolution\wwwroot\chat\code", then you could use string imgPath = Path.Combine(Application.ExecutablePath, #"..\img");
If you have a hard time finding a good anchor, there's a bunch to consider. You can get the path of the executing assembly via Assembly.GetExecutingAssembly().Location
There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.
The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:
Process.Start("Textfile.txt");
So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.
Edit:
If I change above code to this, would it work?
string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");
Windows needs to know where to find the file, so you need somehow specify that:
Either using absolute path:
Process.Start("C:\\1.txt");
Or set current directory:
Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");
Normally CurrentDirectory is set to the location of the executable.
[Edit]
If the file is in the same directory where executable is you can use the code like this:
var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);
The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:
http://www.dotnetperls.com/process-start
However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c
It would look something like this:
string pathPrefix;
if(System.Diagnostics.Debugger.IsAttached())
{
pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
pathPrefix = Application.StartupPath + "\resources\";
}
Process.Start(pathPrefix + "Textfile.txt");
This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.
You'll need to know the current directory if you want to use a relative path.
System.Envrionment.CurrentDirectory
You could append that to your path with Path
System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")
Try using Application.StartupPath path as default path may point to current directory.
This scenario has been explained on following links..
Environment.CurrentDirectory in C#.NET
http://start-coding.blogspot.com/2008/12/applicationstartuppath.html
On a windows box:
Start notepad with the file's location immediately following it. WIN
process.start("notepad C:\Full\Directory\To\File\FileName.txt");
I have to fetch all the files from a folder and i am using the function GetFiles() like
string[] dirImages = Directory.GetFiles(strPathYearImages + intYear , "*.png");
where strPathYearImages="Images\Holiday\2010\"
but when i write the whole path like
string[] dirImages = Directory.GetFiles(#"E:\IWP\Images\Holiday\"+ intYear , "*.png");
i get the required result.
How to solve this problem? I dont want to use the whole path.
Help me out.
Regards,
Jigar <3
The documentation for GetFiles() says:
The path parameter is permitted to
specify relative or absolute path
information. Relative path information
is interpreted as relative to the
current working directory
So you would want to make sure the current working directory is set correctly before trying to use the relative paths (eg to E:\IWP):
GetCurrentDirectory
SetCurrentDirectory
Use Application.StartupPath to get the location where the executable is running. From there you need to know where the images directory is relative to that directory. The only other option is the absolute path. How else would it know where to look?
You can also try using System.IO.Path methods to help - especially for combining path strings, plus it also gives you the location of special folders like My Documents, AppData, and the desktop.
Maybe it's because you are running it from VS inside. Your executable is in ProjectName\bin\Debug, therefore it looks for ProjectName\bin\Debug\Images, which obviously does not exists.
The problem is the first snippet tries to get the images under current path. So you could tell the images path relative to your current path.
Hey all, i got the answer to my question.
I just had to write
string[] dirImages = HttpContext.Current.Server.MapPath(strPathImages + intYear , "*.png");
Hope that it is helpful to all...