Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working on an application developed in C # .net WPF, I wanted to know how to make every launch of my application,
Detect if there is an "Affaires" directory in "%AppData%\Roaming\N.O.E" and, if not, create the directory.
Detect if there is an "Affaires" directory in "C:\N.O.E" and, if there is, prompt the user if they want to move the directory to AppData. If yes, move the directory.
The installation is done with an administrator account.
Should the detection code for my application be in the class "App.XAML.cs"?
Code of the method that launches the application is:
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException +=
new
try
{
// Créé le répertoire des traces s'il n'existe pas déjà
string cwd = Directory.GetCurrentDirectory();
string logPath = Path.Combine(cwd, "log");
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
// Log4Net configuration
FileInfo log4NetConfig = new FileInfo(Path.Combine(cwd,
"Log4net.config"));
log4net.Config.XmlConfigurator.Configure(log4NetConfig);
// Récupère la version de l'application
System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetExecutingAssembly();
Version version = assembly.GetName().Version;
UpdateService.CheckingForUpdates();
Log.Info("Démarrage de l'application " +
OtherHelper.GetAppSetting("NomApplication", "N.O.E") + "
version " + version);
ConfigService.InitializeConfigPathAndModificationDate();
TagService.InitializeReferential();
}
catch (Exception ex)
{
////////
}
base.OnStartup(e);
}
Depending which AppData folder you need, you can retrieve the folder you need using Environment.GetFolderPath().
So, to check for directory "Affaires" in AppData\Roaming\N.O.E create it if it doesn't exist, for example:
string appDataLocalPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string affairesDirPath = appDataRoamingPath + "\\N.O.E.\\Affaires";
if(!Directory.Exists(affairesDirPath))
{
DirectoryInfo affairesDir = Directory.CreateDirectory(affairesDirPath);
//Do anything else you need to with the directory here.
}
If you only need to create the directory without doing anything else with it, then you can simple use Directory.CreateDirectory(affairesDirPath);; it will not create the directory if it already exists.
For your other directory, you can do the following:
string affairesDirPath = "C:\\N.O.E.\\Affaires";
if(Directory.Exists(affairesDirPath))
{
//Move the directory
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am doing recording in my application using C#.
I record voice to the same file and play it but the SoundPlayer plays the contents recorded first time.
For example I have file test.wav where I record "hello" and then I record "hi" to the same file
by overwriting the file. When I play the file test.wav player plays "hello".
I have only one instance of player, e.g.
public static System.Media.SoundPlayer Player;
static void Main()
{
try
{
Player = new System.Media.SoundPlayer();
}
catch (Exception ex)
{
}
}
Code for playing the file:
public static void Play(string fileName)
{
if (File.Exists(fileName))
{
Program.Player.SoundLocation = fileName;
Program.Player.Load();
if (Program.Player.IsLoadCompleted)
{
Program.Player.Play();
}
}
}
I don't know what is wrong here.
Inside the Setter for the SoundLocation property is an interesting check:
set
{
if (value == null)
{
value = string.Empty;
}
if (!this.soundLocation.Equals(value))
{
this.SetupSoundLocation(value);
this.OnSoundLocationChanged(EventArgs.Empty);
}
}
You can see that it looks to see if the new location differs from the old one. If it does, then it does some setup work. If it doesn't, it essentially does nothing.
I'm betting you can get around this by doing something like this:
public static void Play(string fileName)
{
if (File.Exists(fileName))
{
Program.Player.SoundLocation = "";
Program.Player.SoundLocation = fileName;
Program.Player.Load();
if (Program.Player.IsLoadCompleted)
{
Program.Player.Play();
}
}
}
The first call to the SoundLocation setter would clear out the loaded stream. The second one would then set it up properly with the location again and allow for Load to load the stream as expected.
This question already has answers here:
How to split string using Substring
(7 answers)
Closed 8 years ago.
im trying to make c# read text from each line in a txt file then set a variable based on the line this is the code im trying to use
string line;
FileInfo file = new FileInfo("update.txt");
StreamReader stRead = file.OpenText();
while ((line = stRead.ReadLine()) != null)
{
if (line.StartsWith("version=") == true)
{
Version.TryParse(line.Substring(8), out version);
}
if (line.StartsWith("md5=") == true)
{
md5 = line.Substring(4);
}
if (line.StartsWith("url=") == true)
{
url = line.Substring(4);
}
if (line.StartsWith("changelog=") == true)
{
changelog = line.Substring(10);
}
}
stRead.Close();
i put breakpoints in to see what was happening and its reading the txt file but not setting the variables for some reason i declared these variables above the code
private Version version;
private string md5;
private string url;
private string changelog;
only the version variable gets set please help thanks
oh and this is the test txt im using
version=1.1.0.0
md5=564C8AACFBDAA1F5A0AA44A85C53BF55
url=fbnfhbcfn
changelog=bug fixes
The code is fine the way it is. Some things to keep in mind:
The text file update.txt needs to be in the same directory as the executable. So, if a console application or a windows application it should be in bin/Debug, when you start debugging for instance. If you have included it in your project, ensure that the build action is set to "Content", and "Copy to Output Directory" is set to "Always". (This is found under the properties of the file in the solution).
The file update.txt needs to be readable by the executable/runtime
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my code:
public static bool createFile(string dir) {
dir="c:\\e.bat";
System.IO.File.Create(dir);
if (System.IO.File.Exists(dir))
{
try
{
StreamWriter SW;
SW = System.IO.File.CreateText(dir);
SW.WriteLine("something ");
SW.Close();
}
catch (Exception e)
{
Console.Write(e.Message);
Console.ReadLine();
return false;
}
}
return true;
}
here dir is the current directory. i am facing the error The process cannot access the file because it is being used by another process.how can i solve this problem?
You're calling File.Create at the start of the method - which is returning you a stream, which stays open. It's not clear why you're calling that at all, but I'd suggest just removing that line.
You should also use a using statement, only catch specific exceptions, use appropriate using directives, and follow .NET naming conventions. For example:
using System.IO;
...
public static bool CreateFile(string file)
{
using (var writer = File.CreateText(file))
{
try
{
writer.WriteLine("something ");
}
catch (IOException e)
{
// TODO: Change the handling of this. It's weird at the moment
Console.Write(e.Message);
Console.ReadLine();
return false;
}
}
return true;
}
I've removed the check for the file existing, as with the previous code it would always exist because you'd just created it.
You should also consider using File.WriteAllText as a simpler way of writing the file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is my code:
private void Instalarbtn_Click(object sender, RoutedEventArgs e)
{
string MinecraftFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/.minecraft";
string destinationFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "system.zip");
FastZip FastZip = null;
if (Directory.Exists(MinecraftFolder + "/temp"))
{
Directory.Delete(MinecraftFolder + "/temp", true);
}
FastZip.ExtractZip(MinecraftFolder + "/bin/minecraft.jar", MinecraftFolder + "/temp/Minecraft", String.Empty);
try
{
Directory.Delete(MinecraftFolder + "/temp/Minecraft/META-INF", true);
}
catch (DirectoryNotFoundException e1)
{
}
FastZip.ExtractZip(destinationFile, MinecraftFolder + "/temp", String.Empty);
FastZip.CreateZip(MinecraftFolder + "/bin/minecraft.jar", MinecraftFolder + "/temp/Minecraft", true, String.Empty);
if (Directory.Exists(MinecraftFolder + "/temp"))
{
Directory.Delete(MinecraftFolder + "/temp", true);
MessageBox.Show("Instalado correctamente", "Instalador");
}
}
(Sorry for the long code)
If helps i´m trying to extract a .jar, add content at the same folder and repack again.
It doesnt works
Anyone knows because don´t works?
And if know the solution, please tell me
Thanks
EDIT: With this code i want unzip all folders from minecraft.jar, then add some files with overwrite the files and repack again
Since you're not specific about your problem, I'll choose the first issue I see:
string destinationFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "system.zip");
You might not have write permissions for the directory this will refer to. This could be one cause of failure. Run your application as administrator, or change the directory you will write to, and if that works, this may very well be your problem.
Please be more specific...
Change this and debug, try to see the error description of the exception:
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
The tag shows C#.
If so, your folder separators are wrong.
If in doubt, use Path.Combine.
Here's a version using that (and a few extra variables, so the code is easier to read):
private void Instalarbtn_Click(object sender, RoutedEventArgs e) {
string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string MinecraftFolder = Path.Combine(appFolder, "minecraft");
string destinationFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "system.zip");
FastZip FastZip = null;
string minecraftTemp = Path.Combine(MinecraftFolder, "temp");
if (Directory.Exists(minecraftTemp)) {
Directory.Delete(minecraftTemp, true);
}
string minecraftBin = Path.Combine(MinecraftFolder, "bin");
string minecraftTempMinecraft = Path.Combine(minecraftTemp, "Minecraft");
FastZip.ExtractZip(minecraftBin, minecraftTempMinecraft, String.Empty);
string minecraftTempMinecraftMETAINF = Path.Combine(minecraftTempMinecraft, "META-INF");
try {
Directory.Delete(minecraftTempMinecraftMETAINF, true);
} catch (DirectoryNotFoundException e1) {
}
FastZip.ExtractZip(destinationFile, minecraftTemp, String.Empty);
string minecraftBinMinecraftJar = Path.Combine(minecraftBin, "minecraft.jar");
FastZip.CreateZip(minecraftBinMinecraftJar, minecraftTempMinecraft, true, String.Empty);
if (Directory.Exists(minecraftTemp)) {
Directory.Delete(minecraftTemp, true);
MessageBox.Show("Instalado correctamente", "Instalador");
}
}
I have no idea if it works. I do not have or use this FastZip utility, and I did not even bother trying to understand the logic.
You probably have to create a new instance of FastZip, you're currently assigning null to it.
FastZip FastZip = null;
Replace with:
FastZip FastZip = new FastZip();
I recommend you change your FastZip instance's name also (currently called FastZip to more easily recognizable fastZip to distinct it from calling FastZip's (possible) static methods instead of the methods of the actual instance.
FastZip fastZip = new FastZip();